0Pricing
JavaScript Academy · Lesson

Traversing Parents, Children and Siblings

Navigate the DOM tree between related nodes.

Why Traverse

Once you have one element, you often need its relatives: the container around it, the items inside it, or the element right next to it. DOM traversal properties let you move around the tree.

parentElement

parentElement gives you the element that contains the current one, or null at the top of the tree.

const item = document.querySelector(".item");
const container = item.parentElement;
console.log(container.className);

All lessons in this course

  1. Selecting Elements with querySelector
  2. getElementById and getElementsByClassName
  3. Traversing Parents, Children and Siblings
  4. NodeList vs HTMLCollection
← Back to JavaScript Academy