Let’s say you want to go to your neighbor’s house. What’s the fastest and most efficient way to get there?
Move from your house to their house (since you already know their address)
Lookup their address on Google maps, then walk according to the directions Google gives you
If you move directly from your house to their house, you’re doing the equivalent of traversing the DOM—selecting one element from a neighboring element.
If you lookup their address on Google, you’re doing the equivalent of document.querySelector to find elements.
You probably know the answer—it’s always easier to move from an element to another (compared do doing a full search). That’s why we traverse the DOM.
You can traverse in three directions:
Downwards
Sideways
Upwards
Traversing downwards
There are three ways to traverse downwards:
querySelector or querySelectorAll
children
firstElementChild
querySelector or querySelectorAll
To traverse downwards from a specific element, you can use element.querySelector or element.querySelectorAll. You already know this.
If we put element.querySelector into the house analogy, we search for a specific room in your house. It’s faster than searching for the same room from outer space (the document).
children is a property that lets you select direct descendants (elements that are immediately nested in another element). It returns a HTML Collection that updates when children elements are changed.
const list = document.querySelector('.list')
const listItems = list.children
console.log(listItems)
A HTML Collection is a list of HTML Elements. A NodeList is a list of nodes. You can say that a HTML Collection is a subset of a NodeList.
Functions wise, HTML Collections don’t have the forEach method. If you want to loop over a HTML Collection with forEach, you need to convert it into an array with Array.from.
const array = Array.from(HTMLCollection)
array.forEach(el => { /* do whatever you want */})
Selecting a specific child
You can select the nth-item in the list from both NodeLists (result from querySelectorAll) and HTML Collections (result from children). To do so, you use the index of the element, just like how you select a specific item from an Array.
parentElement is great for selecting one level upwards. To find an element that can be multiple levels above the current element, you use the closest method.
closest lets you select the closest ancestor element that matches a selector. Here’s the syntax:
const closestAncestor = Element.closest(selector)
As you may suspect, selector is the same selector you pass to querySelector and querySelectorAll.
In the following HTML, you can select .list from the <a> effortlessly with Element.closest:
Note: closeststarts searching from the current element, then proceeds upwards until it reaches the document. It stops and returns the first element it finds.
closest is pretty new. It doesn’t work on IE Edge 14 and below. It doesn’t work on Opera mini too. If you need to support older browsers, you may want to use a polyfill.
Traversing sideways
There are three ways to traverse sideways:
nextElementSibling
previousElementSibling
Combining parentElement, children, and index
nextElementSibling
You can select the next element with nextElementSibling.
This method lets you select a specific sibling. It’s easier to explain how it works with an example, so let’s do that. Say you want to select the fourth item from the first item in this HTML.
To select the fourth item, you can use firstItem.parentElement to get the list, then list.children to get a HTML Collection. Once you have the HTML Collection, you can find the fourth item by using a index of 3. (Remember, zero-based index!).
You learned how to traverse the DOM in three directions—downwards, upwards, and sideways—in this lesson. Here’s a quick bullet point to summarize the methods you learned:
Traversing downwards
element.querySelector
element.querySelectorAll
element.children
Traversing upwards
element.parentElement
element.closest
Traversing sideways
element.nextElementSibling
element.previousElementSibling
Combine parentElement, children, and index
Exercise
Practice traversing the DOM with the methods taught in this lesson. With the HTML given below, do these tasks:
Select .characters with document.querySelector
Select .humans from .characters
Select all humans with querySelectorAll, starting from .humans