Push a DOM element down one level?

K

Kevin Smith

I want to take a document that contains a number of tables and wrap the
tables inside new elements. I have tried something similar to the
following:

tparent = table.parentNode;
wrapper = document.createElement('DIV');
wrapper.appendChild(table);
tparent.replaceChild(wrapper,table);

This doesn't seem to work though (tested in Safari).
 
L

Lasse Reichstein Nielsen

Kevin Smith said:
I want to take a document that contains a number of tables and wrap the
tables inside new elements. I have tried something similar to the
following:

tparent = table.parentNode;
wrapper = document.createElement('DIV');
wrapper.appendChild(table);

A DOM node can only have one parent. At this point, you *move* the
table into the wrapper ...
tparent.replaceChild(wrapper,table);

.... so it is no longer a child of tparent.

Try switching the operations:

tparent = table.parentNode;
wrapper = document.createElement('DIV');
tparent.replaceChild(wrapper,table);
wrapper.appendChild(table);

/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,335
Latest member
Tommiesal

Latest Threads

Top