HTML DOM TreeWalker nextSibling() method

The TreeWalker nextSibling() method moves the current Node to its next sibling, if any, and returns the found sibling. If no such child exists in the document, this method returns null.


node = treeWalker.nextSibling();

Parameters: This method takes no parameters.

Return Value:

  • Returns next visible sibling of current node if exists.
  • Returns null if no such child exists.

Example: In this example, I have created a TreeWalker with body node and hence showed the next visible sibling of that TreeWalker’s first node.





<!doctype html>
    <meta charset="utf-8">
<title>HTML DOM TreeWalker nextSibling() method</title>    
    <p>Click Below</p>
    <button onclick="get()">Click</button>
        var treeWalker = document.createTreeWalker(
        { acceptNode: function(node) { 
return NodeFilter.FILTER_ACCEPT; } },
        function get(){
            var node = treeWalker.nextSibling();



Before Button Click:

After Button Click: In console, the next visible sibling of  body’s first element of the TreeWalker node that is <p> tag can be seen.

Supported Browsers:

  • Google Chrome
  • Edge
  • Firefox
  • Safari
  • Opera
  • Internet Explorer


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using or mail your article to See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at to report any issue with the above content.