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
- Selecting Elements with querySelector
- getElementById and getElementsByClassName
- Traversing Parents, Children and Siblings
- NodeList vs HTMLCollection