DOM: Re-inserting an element without removing it first

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Stanimir Stamenkov

    #1

    DOM: Re-inserting an element without removing it first

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

    element.parentN ode.insertBefor e(element, beforeElement);

    where 'beforeElement' is one of the 'element.parent Node.childNodes '?

    First, I've used:

    var parentElement = element.parentN ode;
    parentElement.r emoveChild(elem ent);
    parentElement.i nsertBefore(ele ment, beforeElement);

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

    --
    Stanimir
  • Martin Honnen

    #2
    Re: DOM: Re-inserting an element without removing it first



    Stanimir Stamenkov wrote:
    [color=blue]
    > I've wondered if it is right thing to do:
    >
    > element.parentN ode.insertBefor e(element, beforeElement);
    >
    > where 'beforeElement' is one of the 'element.parent Node.childNodes '?
    >
    > First, I've used:
    >
    > var parentElement = element.parentN ode;
    > parentElement.r emoveChild(elem ent);
    > parentElement.i nsertBefore(ele ment, beforeElement);
    >
    > but then I've noticed it works without the remove statement, too.[/color]

    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 DocumentFragmen t 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/

    Comment

    Working...