JavaScript Curriculum
Traversing the DOM
easyA customer clicks the 'Remove' button inside a menu card. The button doesn't know which card it's in. Traversal lets it walk up the tree to find its parent .card and remove the whole thing.
Traversing the DOM
Selecting elements with querySelector every time is slow and fragile. Once you have one element, you can navigate the entire tree from it — up, down, and sideways.
DOM Traversal Navigator
Click any node in the tree, then pick a traversal property to see where it lands:
<div.cards>Direct parent element
const el = document
.querySelector('div')
el.parentElement
// → <div.cards>The closest() Method
closest() is one of the most useful traversal tools — it walks UP the tree and finds the nearest ancestor matching a CSS selector:
A delete button is deeply nested. Clicking it needs to find its parent .card to remove the whole thing.
<div class="cards">
<div class="card">
<h3>Espresso</h3>
<div class="actions">
<button class="delete-btn">×</button>
</div>
</div>
</div>deleteBtn.addEventListener('click', (e) => {
// Without closest — fragile:
// e.target.parentElement.parentElement.parentElement
// With closest — robust:
const card = e.target.closest('.card')
card.remove()
})Step-by-Step Tree Walk
Follow the traversal path from a single starting element:
Select your starting element with querySelector.
const card = document.querySelector('.card')
// → <div class="card">Your Challenge
In the console, select the first .card. Then: log card.parentElement.className, log card.firstElementChild.textContent, log card.nextElementSibling?.tagName, and use card.closest('main') to find the main element.
Challenge
Select a .card element. From it, navigate to its parent using parentElement, its first child using firstElementChild, and its next sibling using nextElementSibling. Then use closest() to find the nearest <main> ancestor.
Traversing the DOM
easyA customer clicks the 'Remove' button inside a menu card. The button doesn't know which card it's in. Traversal lets it walk up the tree to find its parent .card and remove the whole thing.
Traversing the DOM
Selecting elements with querySelector every time is slow and fragile. Once you have one element, you can navigate the entire tree from it — up, down, and sideways.
DOM Traversal Navigator
Click any node in the tree, then pick a traversal property to see where it lands:
<div.cards>Direct parent element
const el = document
.querySelector('div')
el.parentElement
// → <div.cards>The closest() Method
closest() is one of the most useful traversal tools — it walks UP the tree and finds the nearest ancestor matching a CSS selector:
A delete button is deeply nested. Clicking it needs to find its parent .card to remove the whole thing.
<div class="cards">
<div class="card">
<h3>Espresso</h3>
<div class="actions">
<button class="delete-btn">×</button>
</div>
</div>
</div>deleteBtn.addEventListener('click', (e) => {
// Without closest — fragile:
// e.target.parentElement.parentElement.parentElement
// With closest — robust:
const card = e.target.closest('.card')
card.remove()
})Step-by-Step Tree Walk
Follow the traversal path from a single starting element:
Select your starting element with querySelector.
const card = document.querySelector('.card')
// → <div class="card">Your Challenge
In the console, select the first .card. Then: log card.parentElement.className, log card.firstElementChild.textContent, log card.nextElementSibling?.tagName, and use card.closest('main') to find the main element.
Challenge
Select a .card element. From it, navigate to its parent using parentElement, its first child using firstElementChild, and its next sibling using nextElementSibling. Then use closest() to find the nearest <main> ancestor.