Easy Tutorial
❮ Met Cdata Replacedata Prop Element Firstchild ❯

XML DOM nextSibling Property



Definition and Usage

The nextSibling property returns the node immediately following the specified element (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 blank spaces or new lines between nodes as text nodes, while Internet Explorer will ignore blank text nodes generated between 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. Therefore, if the next sibling node is not an element node, it will move to the next node and continue checking if this node is an element node. This process will continue until the next sibling element node is found. By using this method, we can obtain correct results in all browsers.

Tip: For more information on browser differences, please 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_previoussibling(n)
{
  x=n.previousSibling;
  while (x.nodeType!=1)
  {
    x=x.previousSibling;
  }
  return x;
}

xmlDoc=loadXMLDoc("books.xml");

x=xmlDoc.getElementsByTagName("author")[0];
document.write(x.nodeName);
document.write(" = ");
document.write(x.childNodes[0].nodeValue);

y=get_previoussibling(x);

document.write("<br>Previous sibling: ");
document.write(y.nodeName);
document.write(" = ");
document.write(y.childNodes[0].nodeValue);

The above code will output:


Try It Demos

previousSibling - Get the Previous Sibling Node


❮ Met Cdata Replacedata Prop Element Firstchild ❯