Open In App
Related Articles

HTML DOM lastElementChild Property

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

The DOM lastElementChild property is used to return the last child element of the specified element or null if there is no last element. It is similar to the lastChild property but the difference is that the lastChild property returns all last-child nodes i.e. it includes text and comment nodes as well but on the other hand, lastElementChild returns the last node as an element node only. It is a read-only property. 

Syntax:

element.lastElementChild

Return Value: It returns an object which represents the last child of an element. If there is no child element then it returns null. 

Example: In this article, we will use DOM lastElementChild property

HTML

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

                    

Output: 

Supported Browsers: The browser supported by DOM lastElementChild property are listed below:

  • Google Chrome 1.0
  • Edge 12.0
  • Internet Explorer 9.0
  • Firefox 3.5
  • Opera 10.0
  • Safari 4.0


Last Updated : 30 Jun, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads