lastElementChild property in javascript
lastElementChild property is used to return the last child of an element.
If there is no child element it returns null.
Syntax
var elem = element.lastElementChild;
Example Explanation
- In the above example we have created a div (idDiv), it is the parent element.
- Within the parent element, we have added two child elements (div, p).
- When the button is clicked, fun_name() function is called.
- In the Javascript function, first we are getting the parent element (document.getElementById("idDiv")) and storing it in attr variable.
- Using the lastElementChild property, we are getting the last child element and displaying it (alert (attr.lastElementChild)).