HTML DOM lastElementChild Property

Example

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

const element = document.getElementById("myList")
let html = element.lastElementChild.innerHTML;
Try it Yourself »

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

const element =document.getElementById("myDIV")
let tag = element.lastElementChild.tagName;
Try it Yourself »

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

const element = document.getElementById("mySelect")
let text = element.lastElementChild.text;
Try it Yourself »

Description

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

The lastElementChild property is read-only.

Nodes vs Elements

In the HTML DOM terminology:

Nodes are all nodes (element nodes, text nodes, and comment nodes).

Whitespace between elements are also text nodes.

Elements are only element nodes.


childNodes vs children

childNodes returns child nodes (element nodes, text nodes, and comment nodes).

children returns child elements (not text and comment nodes).


firstChild vs firstElementChild

firstChild returns the first child node (an element node, a text node or a comment node). Whitespace between elements are also text nodes.

firstElementChild returns the first child element (not text and comment nodes).


lastChild vs lastElementChild

lastChild returns the last child node (an element node, a text node or a comment node). Whitespace between elements are also text nodes.

lastElementChild returns the last child element (not text and comment nodes).



Syntax

element.lastElementChild

Return Value

Type Description
NodeThe last child element of the element.
nullif no child element exists.

Browser Support

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

It is fully supported in all modern browsers:

Chrome Edge Firefox Safari Opera IE
Yes Yes Yes Yes Yes 11

Copyright 1999-2023 by Refsnes Data. All Rights Reserved.