Open In App

HTML DOM TreeWalker currentNode Property

Last Updated : 11 Jul, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

The TreeWalker currentNode property represents the Node on which the current TreeWalker is pointing.

Syntax:

To get the currentNode:

node = treeWalker.currentNode;

To set the currentNode:

treeWalker.currentNode = node;

Return Value: This method returns the current node of the current TreeWalker.

Example 1: In this example, we will get the currentNode of the TreeWalker. For this, we had created a TreeWalker with node head, to getting the currentNode.

HTML




<!DOCTYPE html>
<html>
 
<body>
    <h1>GeeksforGeeks</h1>
 
     
<p>Click Here to get the currentNode</p>
 
 
    <button onclick="get()">Click</button>
 
    <script>
        var treeWalker = document
            .createTreeWalker(document.head);
             
        function get() {
            node = treeWalker.currentNode;
            console.log(node);
        }
    </script>
</body>
 
</html>


Output:

Before Clicking the Button:

After Clicking the Button:

Example 2: In this example, we will set the currentNode of created TreeWalker to body.

HTML




<!DOCTYPE html>
<html>
 
<body>
    <h1>GeeksforGeeks</h1>
 
     
<p>
        Click Here to set the
        currentNode to body
    </p>
 
 
    <button onclick="set()">Click</button>
 
    <script>
        var treeWalker = document
            .createTreeWalker(document.head);
             
        function set() {
            treeWalker.currentNode = document.body;
            console.log(treeWalker);
            console.log(treeWalker.currentNode);
        }
    </script>
</body>
 
</html>


Output:

Before Clicking the Button:

After Clicking the Button:

Supported Browsers:

  • Google Chrome 1
  • Edge 12
  • Firefox 4
  • Safari 3
  • Opera 9
  • Internet Explorer 9


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads