HTML DOM Element lastElementChild property

Definition and usage

lastElementChild The property returns the last child element of the element.

lastElementChild The property is read-only.

See also:

children property

firstElementChild property

childElementCount property

tagName property

childNodes property

HTML node and element

In HTML DOMIn the (Document Object Model), an HTML document is a collection of nodes (with or without child nodes).

Noderefers to element nodes, text nodes, and comment nodes.

ElementWhitespaces between them are also text nodes.

And elements are just element nodes.

Child Nodes vs Child Elements

childNodes ReturnsChild Nodes(Element nodes, text nodes, and comment nodes).

children ReturnsChild Elements(Instead of text and comment nodes).

firstChild vs firstElementChild

firstChild Returns the first oneChild Nodes(Element nodes, text nodes, or comment nodes). Whitespace between elements is also a text node.

firstElementChild Returns the first oneChild Elements(It does not return text nodes and comment nodes).

lastChild vs lastElementChild

lastChild Returns the last oneChild Nodes(Element nodes, text nodes, or comment nodes). Whitespace between elements is also a text node.

lastElementChild Returns the last oneChild Elements(It does not return text nodes and comment nodes).

Example

Example 1

Get the HTML content of the last child element of the <ul> element:

const element = document.getElementById("myList")
let html = element.lastElementChild.innerHTML;

Try it yourself

Example 2

Get the tag name of the last child element of the <div> element:

const element =document.getElementById("myDIV")
let tag = element.lastElementChild.tagName;

Try it yourself

Example 3

Get the text of the last child element of the <select> element:

const element = document.getElementById("mySelect")
let text = element.lastElementChild.text;

Try it yourself

Syntax

element.lastElementChild

Return Value

Type Description
Node The last child element of the element.
null If there is no child.

Browser Support

element.lastElementChild It is a DOM Level 3 (2004) feature.

All browsers fully support it:

Chrome IE Edge Firefox Safari Opera
Chrome IE Edge Firefox Safari Opera
Support 9-11 Support Support Support Support