English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM lastElementChild attribute

HTML DOM Element Object

ThelastElementChildA read-only attribute that returns the last child element of the specified parent element.

If the parent element has no child elements, this method will returnnullvalue.

UsechildrenAttribute returns any child element of the specified parent element.

To return the first child element of the specified parent element, usefirstElementChildAttribute.

Syntax:

ParentElement.lastElementChild
<div>
   <p>This is the1a P</p>
   <p>This is the2a P</p>
   <p>This is the3a P</p>
</div>
<script>
var x = document.querySelector("div").lastElementChild.nodeName;
document.getElementById("result").innerHTML = x;
</script>
Test and See‹/›

Browser compatibility

All browsers fully support the lastElementChild attribute:

Attribute
lastElementChildisisisisis

Technical details

Return value:A Node object representing the last child element of the specified parent element; if there are no child elements, it isnull
DOM Version:DOM Level1

More Examples

Get the HTML content of the last child element of the DIV element:

<div>
   <p>This is the1a P</p>
   <p>This is the2a P</p>
   <p>This is the3a P</p>
</div>
<script>
var x = document.querySelector("div").lastElementChild.textContent;
document.getElementById("result").innerHTML = x;
</script>
Test and See‹/›

Change the HTML content of the last child element of the DIV element:

var div = document.querySelector("div");
div.lastElementChild.textContent = "HELLO WORLD";
Test and See‹/›

Related References

HTML DOM Reference:firstElementChild property

HTML DOM Element Object