Connecting Tech Pros Worldwide Forums | Help | Site Map

Push a DOM element down one level?

Kevin Smith
Guest
 
Posts: n/a
#1: Jul 20 '05
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).

--
Kevin Smith
Kevin.Smith@sas.com

Lasse Reichstein Nielsen
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Push a DOM element down one level?


Kevin Smith <Kevin.Smith@sas.com> writes:
[color=blue]
> 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);[/color]

A DOM node can only have one parent. At this point, you *move* the
table into the wrapper ...
[color=blue]
> tparent.replaceChild(wrapper,table);[/color]

.... 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
--
Lasse Reichstein Nielsen - lrn@hotpop.com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
Closed Thread


Similar JavaScript / Ajax / DHTML bytes