Skip to main content Accessibility Feedback

Traversing the DOM

Element.firstElementChild & Element.lastElementChild

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

	<!-- The gray wizard -->
	<!-- The surprise hero -->
let wizards = document.querySelector('ul');

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

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

Join the Lean Web Club! Coaching. Courses. Coding resources. Get the skills, confidence, and support you need to learn front-end web development and achieve long-term success.

Find this useful? You can support my work by purchasing an annual membership.