Skip to content
Justice Tech School

Navigating the DOM

Move around with parentElement, children, firstElementChild, nextElementSibling and closest. Prefer element properties over node ones to skip text nodes.

Example
const ul = document.createElement("ul");
ul.innerHTML = "<li>A</li><li>B</li>";
document.body.appendChild(ul);
console.log(ul.children.length, ul.firstElementChild.textContent);
console.log(ul.lastElementChild.previousElementSibling.textContent);

Exercise

Parent element.

el.Element

Quiz Test

Which finds the nearest matching ancestor?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.