473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 1394


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Kookymon1 | last post by:
This is an attempt to respond to an older question (several months). Date: 2002-03-07 13:10:23 PST Subject: On the Common DOM API and Applets. The original message was: >LiveConnect and the...
0
by: Steve Roberts | last post by:
I want to write a graphical view of an XML tree, using a DOM interface, that allows the XML tree to be edited, with nodes being added or removed. For this I was going to create a basic tree...
0
by: Thorsten Meininger | last post by:
As far as I know one could access DOM trees through "pure" DOM and through JAXP using DOM. I have seen dozends of examples but I could not find any differences between the two methods. Which java...
7
by: Ganesh Gella | last post by:
Hi All, I am planning to use Xalan to transform XML data by applying xls stylesheets. Here tricky part is, Xalan provides several C++ APIs, which are very much useful if our requirement is...
4
by: Robert | last post by:
Hi, which DOM version (level) should I learn? I've seen that there is a DOM level 3, but is there any support for it already? -- Rob
5
by: segfalt | last post by:
Hey All, I'm wondering if someone can give me a hand with the following. I'm frequently updating xml files, sometimes in bulk updates. I'm having trouble getting the output formatted nicely...
1
by: pbd22 | last post by:
hi. for some reason i am not able to navigate the dom. i keep getting the 'no properties' error but i think i am doing everything right. this is an XML response from an AJAX call. The ajax...
3
by: steven | last post by:
Hi... I'm "ajaxifying" my image gallery and I have some code which creates the elements on the fly and places them on the page. Using firefox, the code works fine and the elements show up as...
0
by: Greg Copeland | last post by:
I am attempting to freeze an application which uses the dom.minidom parser. When I execute my application, I get an import error of: ImportError: No module named dom.minidom. During the freeze...
9
by: Lie | last post by:
Why this generates AttributeError, then not? Python 2.5.2 (r252:60911, Apr 21 2008, 11:17:30) on linux2 Type "help", "copyright", "credits" or "license" for more information. Traceback (most...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.