The HTML DOM lastElementChild property returns the last element node as node object of the specified node.
NOTE − This property does ignore text and comment nodes
Syntax
Following is the syntax −
Returning lastElementChild node
nodeList.lastElementChild
Example
Let us see an example for HTML DOM lastElementChild property −
<!DOCTYPE html> <html> <head> <title>HTML DOM lastElementChild</title> <style> form { width:70%; margin: 0 auto; text-align: center; } * { padding: 2px; margin:5px; } input[type="button"] { border-radius: 10px; } ul{ width: 30%; margin: 0 auto; } </style> </head> <body> <form> <fieldset> <legend>HTML-DOM-lastElementChild</legend> <h3>Grocery Store</h3> <ul id="dayList"> <li>Monday</li> <li>Tuesday</li> <li>Wednesday</li> <li>Thursday</li> <li>Friday</li> <li>Saturday</li> <li>Sunday</li> </ul> <input type="button" onclick="getTimmings()" value="Get Sale Day"> <div id="divDisplay"></div> </fieldset> </form> <script> var divDisplay = document.getElementById("divDisplay"); var dayList = document.getElementById("dayList"); function getTimmings() { divDisplay.textContent = 'Sale on last day of week: '+dayList.lastElementChild.textContent; } </script> </body> </html>
Output
This will produce the following output −
Before clicking ‘Get Sale Day’ button −
After clicking ‘Get Sale Day’ button −