nextSibling

Summary

Returns the node immediately following the specified one in its parent's childNodes list, or null if the specified node is the last node in that list.

Syntax

nextNode = node.nextSibling

Notes

Gecko-based browsers insert text nodes into a document to represent whitespace in the source markup. Therefore a node obtained, for example, using Node.firstChild() or Node.previousSibling() may refer to a whitespace text node rather than the actual element the author intended to get.

See Whitespace in the DOM and W3C DOM 3 FAQ: Why are some Text nodes empty? for more information.

Example

<div id="div-01">Here is div-01</div>
<div id="div-02">Here is div-02</div>

<script type="text/javascript">
var el = document.getElementById('div-01').nextSibling;

document.write('<p>Siblings of div-01</p><ol>');

while (el) {
  document.write('<li>' + el.nodeName + '</li>');
  el = el.nextSibling;
}

document.write('</ol>');
</script>

/**************************************************
  The following is written to the page as it loads:

     Siblings of div-01

      1. #text
      2. DIV
      3. #text
      4. SCRIPT
      5. P
      6. OL
**************************************************/

In the above example, it can be seen that #text nodes are inserted in the DOM where whitespace occurs in the markup between tags (i.e. after the closing tag of an element and before the opening tag of the next). No whitespace is created between the elements inserted by the document.write statement.

The possible inclusion of text nodes in the DOM must be allowed for when traversing the DOM using nextSibling. See the resources in the Notes section.

Specification

See also

Document Tags and Contributors

Contributors to this page: Dria
Last updated by: Dria,