HTML | DOM lastChild Property

The DOM lastChild property is used to return the last child of the specified node. It returns the last child nodes as text, comment or element node (depends on which occurs at last). It is a read-only property.

Syntax:

node.lastChild

Return Value: It returns a node object which represents the last child of the node and null if there are no child elements.



Example 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>
            DOM lastChild Property
        </title>
          
        <script>
            function geek() {
                var doc = 
                document.getElementById("div").lastChild.innerHTML;
                  
                document.getElementById("p").innerHTML = doc;
                document.getElementById("p").style.color = "white";
                document.getElementById("p").style.background = "green";
            }
        </script>
    </head>
      
    <body style = "text-align: center;">
          
        <h1 style = "color:green;">
            GeeksforGeeks
        </h1>
          
        <h2>
            DOM lastChild Property
        </h2>
          
        <div id = "div">
        <span>GeeksforGeeks! </span>
        <span>
            A computer science portal for geeks.
        </span></div>
        <br>
        <button onclick="geek()">Click me!</button>
          
        <p id="p"></p>
    </body>
</html>                    

chevron_right


Output:
Before click on the button:
lastChild
After click on the button:
lasChild

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>
            HTML DOM lastChild Property
        </title>
          
        <script>
            function Geeks() {
                var x = 
                document.getElementById("sel").lastChild.text;
                  
                document.getElementById("p").innerHTML 
                        = "Last child: " + x;
            }
        </script>
    </head>
      
    <body style = "text-align: center;">
          
        <h1 style = "color:green;">
            GeeksforGeeks
        </h1>
          
        <h2>
            DOM lastChild Property
        </h2>
          
        <select id = "sel" size = "3">
            <option>Merge sort</option>
            <option>Quick sort</option>
            <option>Insertion sort</option></select>
        <br><br>
          
        <button onclick = "Geeks()">
            Click Here!
        </button>
          
        <p id="p"></p>
    </body>
</html>                    

chevron_right


Output:
Before click on the button:
lastChild
After click on the button:
lasChild
Supported Browsers: The browser supported by lastChild property are listed below:

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


My Personal Notes arrow_drop_up


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.