# The DOM: Selecting & Traversing

> Learn how to find and navigate between HTML elements using JavaScript. Master modern query selectors and tree traversal properties.

Subject: JavaScript | Lesson 41 of 63 | 6 min read
Interactive lesson: https://codewithanimation.in/javascript/dom-selection-and-traversal
Previous: [The DOM Tree](https://codewithanimation.in/javascript/document-object-model.md) | Next: [DOM Manipulation](https://codewithanimation.in/javascript/dom-manipulation.md)

## Finding Elements

Before your JavaScript can change an element on the screen, it first has to find it inside the massive DOM tree.

Historically, developers used specialized methods like `document.getElementById('nav')` or `document.getElementsByClassName('card')`.

## The Modern Query Engine

Today, we use two universal methods that allow you to find elements using standard CSS selectors:

1. `document.querySelector('.card > p')` returns the **first** element that matches the CSS selector.

2. `document.querySelectorAll('.card')` returns a **NodeList** containing *all* elements that match the selector.

## Walking the Tree (Traversal)

Sometimes you don't want to search the entire document. If you already have a reference to a specific element, you can "walk" up and down the tree relative to that element.

You can move up using `element.parentElement`.

You can move down using `element.children` (all children) or `element.firstElementChild`.

You can move sideways using `element.nextElementSibling` or `element.previousElementSibling`.

## Common mistake: NodeLists vs Arrays

`querySelectorAll` returns a `NodeList`, not a true JavaScript Array.

While modern browsers allow you to use `.forEach()` on a NodeList, it does *not* have array methods like `.map()`, `.filter()`, or `.reduce()`. If you need those, you must convert it into a real array first using `Array.from(nodeList)` or the spread operator `[...nodeList]`.

## Tip: Scoping Queries

You don't always have to query from the global `document`. You can query *inside* a specific element to improve performance: `const mySection = document.querySelector('#hero'); const btn = mySection.querySelector('.btn');`

## Try it: The Query Engine

Click the query commands below to run them against the simulated DOM tree. Watch how different selectors and traversal methods light up the exact nodes they target.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/javascript/dom-selection-and-traversal

## Check yourself

### Question 1

What does `document.querySelector('.highlight')` return?

- A. The very first element it finds with the class 'highlight'.
- B. An array of all elements with the class 'highlight'.
- C. It returns an error if there are multiple matches.

Correct answer: A

`querySelector` stops searching the DOM tree the moment it finds the first match, making it fast and predictable.

- Why not B: Incorrect. querySelector only ever returns a single element. You want querySelectorAll.
- Why not C: Incorrect. It just silently returns the first one and ignores the rest.

### Question 2

If you have a `<li>` element and want to find the `<ul>` that contains it, which property should you use?

- A. element.parentElement
- B. element.previousElementSibling
- C. element.children

Correct answer: A

`parentElement` walks exactly one step UP the DOM tree to the immediate container.

- Why not B: Incorrect. That moves sideways to the previous <li>, not upwards.
- Why not C: Incorrect. That moves downwards into the children of the <li>.

### Question 3

How can you use `.map()` on the result of `document.querySelectorAll('div')`?

- A. Convert it to an array first: Array.from(document.querySelectorAll('div')).map(...)
- B. Just call it directly: document.querySelectorAll('div').map(...)
- C. NodeLists cannot be mapped.

Correct answer: A

`querySelectorAll` returns a NodeList. While it looks like an array and has a `length` property, it lacks advanced array methods until explicitly converted.

- Why not B: Incorrect. A NodeList does not have a .map() method on its prototype.
- Why not C: Incorrect. They can be mapped once converted to a real Array.

## Remember this

- `querySelector` returns the first matching element.
- `querySelectorAll` returns a NodeList of all matching elements.
- A NodeList is not an Array; use `Array.from()` to convert it if you need `.map()`.
- Traverse upwards using `parentElement`.
- Traverse sideways using `nextElementSibling` or `previousElementSibling`.
