HTML DOM lastElementChild
Property
Example
Get the last child element of a <ul> element in HTML:
var x = document.getElementById("myList").lastElementChild.innerHTML;
Definition and Usage
The lastElementChild
property returns the last child element of the specified element.
The lastElementChild
property is different from the lastChild property in that lastChild
returns the last child node as an element node, including text nodes or comment nodes (depending on which is last), while lastElementChild
returns the last child node as an element node (ignoring text and comment nodes).
This property is read-only.
Tip: Use the children
property to return any child elements of the specified element.
Tip: To return the first child element of the specified element, use the firstElementChild property.
Browser Support
Property | Chrome | IE | Firefox | Safari | Opera |
---|---|---|---|---|---|
lastElementChild | 2.0 | 9.0 | 3.5 | 4.0 | 10.0 |
Syntax
element.lastElementChild
Parameters
None
Return Value
Type | Description |
---|---|
Object | Returns a Node object representing the last child element of the element, or null if there are no child elements. |
Technical Details
| DOM Version | Core Level 3 Element Traversal | | --- | --- |
More Examples
Example
Get the tag name of the last child element of a <div> element:
var x = document.getElementById("myDIV").lastElementChild.tagName;
document.getElementById("demo").innerHTML = x;
Example
Get the text of the last child element of a <select> element:
var x = document.getElementById("mySelect").lastElementChild.text;