Skip to content
Related Articles

Related Articles

HTML DOM TreeWalker firstChild() method

View Discussion
Improve Article
Save Article
  • Last Updated : 11 Jul, 2022

The TreeWalker firstChild() method moves the current Node to the first visible child of the current node, and returns the first found child.If no such child exists in the document, this method returns null.


node = treeWalker.firstChild();

Parameters: This method takes no parameters.

Return Value: 

  • Returns firstChild of the node if exists.
  • Returns null if no such child exists.

Example: In this example, create a TreeWalker and hence showed the first child of the TreeWalker node.


<!doctype html>
    <meta charset="utf-8">
<title>HTML DOM TreeWalker firstChild() 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.firstChild();


Before Button Click:

After Button Click:

Supported Browsers:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 4 and above
  • Safari 3 and above
  • Opera 9 and above
  • Internet Explorer 9 and above
My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!