Skip to main content

Node.firstElementChild & Node.lastElementChild

Get the first and last child element of a parent element.

<ul>
<!-- The gray wizard -->
<li>Gandalf</li>
<li>Radagast</li>
<li>Hermione</li>
<!-- The surprise hero -->
<li>Neville</li>
</ul>
let wizards = document.querySelector('ul');

// returns <li>Gandalf</li>
let firstDescendant = wizards.firstElementChild;

// returns <li>Neville</li>
let lastDescendant = wizards.lastElementChild;