469,613 Members | 1,274 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,613 developers. It's quick & easy.

DOM: Re-inserting an element without removing it first

I've wondered if it is right thing to do:

element.parentNode.insertBefore(element, beforeElement);

where 'beforeElement' is one of the 'element.parentNode.childNodes'?

First, I've used:

var parentElement = element.parentNode;
parentElement.removeChild(element);
parentElement.insertBefore(element, beforeElement);

but then I've noticed it works without the remove statement, too.

--
Stanimir
Jul 23 '05 #1
1 1274


Stanimir Stamenkov wrote:
I've wondered if it is right thing to do:

element.parentNode.insertBefore(element, beforeElement);

where 'beforeElement' is one of the 'element.parentNode.childNodes'?

First, I've used:

var parentElement = element.parentNode;
parentElement.removeChild(element);
parentElement.insertBefore(element, beforeElement);

but then I've noticed it works without the remove statement, too.


Yes, all those DOM methods that add/insert an element remove the element
first if it is already attached to the tree:
http://www.w3.org/TR/DOM-Level-2-Cor...ID-1950641247:
insertBefore
Inserts the node newChild before the existing child node refChild.
If refChild is null, insert newChild at the end of the list of children.
If newChild is a DocumentFragment object, all of its children are
inserted, in the same order, before refChild. If the newChild is already
in the tree, it is first removed.
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Kookymon1 | last post: by
reply views Thread by Steve Roberts | last post: by
reply views Thread by Thorsten Meininger | last post: by
7 posts views Thread by Ganesh Gella | last post: by
4 posts views Thread by Robert | last post: by
5 posts views Thread by segfalt | last post: by
reply views Thread by Greg Copeland | last post: by
9 posts views Thread by Lie | last post: by
reply views Thread by devrayhaan | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.