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 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 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:
<!DOCTYPE html> <html> <head> <title> DOM lastElementChild Property </title> <!-- script to find last child element --> <script> function Geeks() { var doc = document.getElementById("GFG").lastElementChild.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 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> </body> </html> |
Output:
Before click on the button:

After click on the button:

Supported Browsers: The browser supported by DOM lastElementChild property are listed below:
- Google Chrome 2.0
- Internet Explorer 9.0
- Firefox 3.5
- Opera 10.0
- Safari 4.0
Recommended Posts:
- HTML | DOM lang Property
- HTML | DOM Style backgroundClip Property
- HTML | DOM Style borderTop Property
- HTML | DOM Meter max Property
- HTML | DOM Input Hidden value Property
- HTML | DOM Textarea autofocus Property
- HTML | DOM Style columnGap Property
- HTML | DOM Input Time autofocus Property
- HTML | DOM Input Submit formTarget Property
- HTML | DOM Style boxSizing Property
- HTML | DOM Video seekable Property
- HTML | DOM Input Checkbox disabled Property
- HTML | DOM Source src Property
- HTML | DOM Input Range max Property
- HTML | DOM Video loop Property
- HTML | DOM Button disabled Property
- HTML | DOM Input URL size Property
- HTML | DOM Style borderImageSource Property
- HTML | DOM Style wordBreak Property
- HTML | DOM Base target Property
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.


