ZVON > References > DOM1 Reference

insertBefore (method)

Owning interface and usage:  
Node.insertBefore(newChild, refChild)

Member of these other interfaces :  
Attr, CDATASection, CharacterData, Comment, Document, DocumentFragment, DocumentType, Element, Entity, EntityReference, Notation, ProcessingInstruction, Text

Inserts the node newChild before the existing child node refChild. If refChild is null, insert newChild at the end of the list of children.

Node newChild  -  The node to insert.
Node refChild  -  The reference node, i.e., the node before which the new node must be inserted.

Node -  The node being inserted.

Raised if this node is of a type that does not allow children of the type of the newChild node, or if the node to insert is one of this node's ancestors.
Raised if newChild was created from a different document than the one that created this node.
Raised if this node is readonly.
Raised if refChild is not a child of this node.

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.

Text in the first DIV.
Some text in the second DIV.
Some text and element in the third DIV.
We can try another elements. It will be much more interesting.
Text in the last DIV.
       <div id="doc">
           Text in the first DIV.
         <div id="DDD" class="secondClass">
           Some text in the second DIV.
         <div class="thirdClass">
           Some text and <span id="SSS">element</span> in the third DIV.
         <div class="fourthClass">
           We can try <i>another elements</i>.
           It will be much more <b>interesting</b>.
           Text in the last DIV.
  var main = document.getElementById('doc');
  main.insertBefore(document.getElementById('DDD'), main.firstChild);
  var output = main.firstChild.firstChild.nodeValue;
desired your browser
Some text in the second DIV.