XML DOM nextSiblingProperty


Element object

Definition and Usage

The nextSibling property returns the next sibling node of the selected element (the next node at the same tree level).

If no such node exists, this property returns NULL.

Syntax

elementNode.nextSibling

Tips and notes

Note:Firefox and most other browsers treat empty spaces or line breaks generated between nodes as text nodes, while Internet Explorer ignores white space text nodes generated between nodes. Therefore, in the example below, we will use a function to check the node type of the next sibling node.

The node type of an element node is 1, so if the next sibling node is not an element node, it moves to the next node, and then continues to check whether this node is an element node. The entire process continues until the next sibling element node is found. In this way, we can get correct results in all browsers.

Tip:To learn more about browser differences, visit our XML DOM tutorial.DOM browserChapter.


Example

The following code snippet usesloadXMLDoc()put "books.xmlLoad into xmlDoc, and get the next sibling node from the first <title> element:

Example

//Get the next sibling node of an element node function get_nextsibling(n) { x=n.nextSibling; while (x.nodeType!=1) { x=x.nextSibling; } return x; } xmlDoc=loadXMLDoc("books.xml"); x=xmlDoc.getElementsByTagName("title")[0]; document.write(x.nodeName); document.write(" = "); document.write(x.childNodes[0].nodeValue); y=get_nextsibling(x); document.write("<br>Next sibling: "); document.write(y.nodeName); document.write(" = "); document.write(y.childNodes[0].nodeValue);

The above code will output:

title = Everyday Italian
Next sibling: author = Giada De Laurentiis

Try it Yourself »

Try it Yourself

previousSibling - Get the previous sibling node of a node


Element objectother extensions