English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
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.
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‹/›
All browsers fully support the lastElementChild attribute:
Attribute | |||||
lastElementChild | is | is | is | is | is |
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 |
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‹/›
HTML DOM Reference:firstElementChild property