Results 1 to 5 of 5

Thread: Can you append the same node multiple times?

  1. #1
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default Can you append the same node multiple times?

    Hi all,

    Simple quick question: If I create "oChild" with createElement, and then use appendChild to add it to "oParent", can I append oChild to a different element later, so that both parents have an instance of oChild?

    And if so, can I also attach multiple instances of oChild to oParent?

    Thanks!
    Last edited by jlizarraga; 10-28-2008 at 05:59 PM. Reason: resolved

  2. #2
    Join Date
    Jun 2008
    Posts
    589
    Thanks
    13
    Thanked 54 Times in 54 Posts
    Blog Entries
    1

    Default

    I'm pretty sure you can.

    -magicyte

  3. #3
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    In testing, no:

    Code:
    window.onload = function(){
    var el = document.createElement('div');
    el.appendChild(document.createTextNode('Hi'));
    for (var i = 10; i > 0; --i)
    document.body.appendChild(el);
    };
    will append only one node to the body. However, if you clone the node:

    Code:
    window.onload = function(){
    var el = document.createElement('div');
    el.appendChild(document.createTextNode('Hi'));
    for (var i = 10; i > 0; --i)
    document.body.appendChild(el.cloneNode(true));
    };
    That works.

    If you do this though, be careful. If you set an id for the node or anything that later may need to be unique for further processing, there will be problems if and when that unique feature is attempted to be used in that processing.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  4. The Following User Says Thank You to jscheuer1 For This Useful Post:

    jlizarraga (10-28-2008)

  5. #4
    Join Date
    Apr 2008
    Location
    San Diego, CA
    Posts
    352
    Thanks
    57
    Thanked 6 Times in 6 Posts

    Default

    Thanks John!

  6. #5
    Join Date
    Jul 2006
    Posts
    497
    Thanks
    8
    Thanked 70 Times in 70 Posts

    Default

    EDIT: Oops. I'm a little late.

    Quote Originally Posted by http://www.w3.org/TR/DOM-Level-2-Core/core.html#ID-184E7107
    appendChild
    Adds the node newChild to the end of the list of children of this node. If the newChild is already in the tree, it is first removed.
    Quote Originally Posted by https://developer.mozilla.org/en/DOM/element.appendChild
    If child is a reference to an existing node in the document, appendChild moves it from its current position to the new position (i.e. there is no requirement to remove the node from its parent node before appending it to some other node).

    This also means that a node can't be in two points of the document simultaneously. So if the node already has a parent, it is first removed, then appended at the new position.
    Microsoft presumably follows the standard as well, but they don't explicitly say so.
    -- Chris
    informal JavaScript student of Douglas Crockford
    I like wikis - a lot.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •