X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

XML DOM insertBefore() Method


Node Object Reference Node Object

Definition and Usage

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.


Browser Support

Internet Explorer Firefox Opera Google Chrome Safari

The insertBefore() method is supported in all major browsers.


Syntax

nodeObject.insertBefore(newchild,existingnode)

Parameters

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

Return Value

Type Description
Node object The inserted node

Technical Details

DOM Version Core Level 1 Node Object. Modified in DOM Level 3


Example

The following code fragment loads "books.xml" into xmlDoc using loadXMLDoc(), creates a new <book> node and inserts it before the last <book> node:

Example

xmlDoc = loadXMLDoc("books.xml");
newNode = xmlDoc.createElement("book");

x = xmlDoc.documentElement;
y = xmlDoc.getElementsByTagName("book")[3];

x.insertBefore(newNode,y);

Try it yourself »

Node Object Reference Node Object

Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook