HTML DOM TreeWalker lastChild() method

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

Syntax:

node = treeWalker.lastChild();

Parameters: This method takes no parameters.

Return Value:

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

Example: In this example, create a TreeWalker with head node and hence showed the lastchild of that TreeWalker node.



filter_none

edit
close

play_arrow

link
brightness_4
code

<!doctype html>
<html>
<head>
    <meta charset="utf-8">
<title>HTML DOM TreeWalker lastChild() method</title>    
</head>
<body>
    <h1>GeeksforGeeks</h1>
    <p>Click Below</p>
    <button onclick="get()">Click</button>
</body>
<script>
        var treeWalker = document.createTreeWalker(
        document.head,
        NodeFilter.SHOW_ELEMENT,
        { acceptNode: function(node) {
 return NodeFilter.FILTER_ACCEPT; } },
        false
);
        function get(){
            var node = treeWalker.lastChild();
            console.log(node) 
        }
</script>
</html>

chevron_right


Output:

Before Button Click:

After Button Click: In console, the last child of the TreeWalker node that is <title> can be seen.

Supported Browsers:

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

full-stack-img

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 contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. 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 contribute@geeksforgeeks.org to report any issue with the above content.