Easy Tutorial
❮ Met Text Deletedata Prop Attr Nodetype ❯

XML DOM nextSibling Property



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


Tips and Notes

Note: Firefox and most other browsers will treat spaces or newlines between nodes as text nodes, while Internet Explorer will ignore these whitespace text nodes. Therefore, in the following example, we will use a function to check the node type of the next sibling node.

The node type for an element node is 1. So, if the next sibling node is not an element node, it moves to the next node and continues checking if it is an element node. This process continues until the next sibling element node is found. This method ensures correct results across all browsers.

Tip: For more information on browser differences, visit our DOM Browsers section in our XML DOM tutorial.


Example

The following code snippet uses loadXMLDoc() to load "books.xml" into xmlDoc and retrieves 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

previousSibling - Get the previous sibling node


❮ Met Text Deletedata Prop Attr Nodetype ❯