Open In App

HTML DOM NodeIterator nextNode() Method

Improve
Improve
Like Article
Like
Save
Share
Report

The NodeIterator.nextNode() method sets the NodeIterator to next node in the document and returns the nextNode. The first call of the nextNode() returns the first node in the set.

This method returns null when there are no nodes left in the set.

Syntax:

node = nodeIterator.nextNode();

Parameters: This method takes no parameter.

Return Value: This method returns the next node in the set.

Example: In this example, we will create a node iterator and hence will iterate using the nextNode() method.




<!DOCTYPE HTML> 
<html>  
<head>
    <meta charset="UTF-8">
    <title>HTML | DOM nextNode() Method</title>
</head>   
  
<body style="text-align:center;">
    <h1 style="color:green;">  
     GeeksforGeeks
    </h1
    <p
HTML | DOM nextNode() Method
    </p>
  
    <button onclick = "Geeks()">
    Click Here
    </button>
    <p id="a"></p>
    <script
        var a = document.getElementById("a");
        function Geeks(){
           const nodeIterator = document.createNodeIterator(
                document.body,
                NodeFilter.SHOW_ELEMENT
                )
             
            let nextNode=nodeIterator.nextNode();
            nextNode=nodeIterator.nextNode();
            nextNode=nodeIterator.nextNode();
            a.innerHTML =
'Next Node content is : '+nextNode.textContent;
            console.log(nextNode);
}
</script>
</body>
</html>


Output:

Before Clicking Button:

After Clicking Button:

In the console: in Console, the next node can be seen.

Supported Browsers:

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


Last Updated : 23 Jul, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads