The insertBefore() method inserts a new child node before a specified child node of the current node.
Note: If the newchild is already in the tree, it is first removed.
The insertBefore() method is supported in all major browsers.
Parameter | Type | Description |
---|---|---|
newchild | Node object | Required. The new child node to insert |
existingnode | Node object | Required. The node to insert the new child node before. If existingnode is null, insert newchild at the end of the list of children |
Type | Description |
---|---|
Node object | The inserted node |
DOM Version | Core Level 1 Node Object. Modified in DOM Level 3 |
---|
The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc(), creates a new <book> node and inserts it before the last <book> node: