Selecting
DOM Elements
Learn how to find and grab any HTML element on a page using JavaScript's powerful querySelector and querySelectorAll methods.
What is the DOM?
The Document Object Model (DOM) is a tree-like representation of your HTML page that JavaScript can read and modify. Every HTML tag becomes a node in this tree: the page is the root, and each element is a branch or leaf.
Why Select Elements?
Before you can do anything interactive on a web page, you must first grab a reference to the element you want to work with. Selection is the first step in virtually every DOM operation.
CSS Selectors Refresher
Both querySelector and querySelectorAll accept the exact same selector strings you already use in CSS. Here are the three most common types:
"p" selects all <p> elements on the page.".btn" selects elements with class="btn"."#main" selects the single element with id="main".The querySelector Method
The querySelectorAll Method
querySelector vs querySelectorAll
| querySelector | querySelectorAll | |
|---|---|---|
| Returns | Single Element (or null) | NodeList (array-like) |
| How many? | First match only | All matches |
| No match | null | Empty NodeList (length 0) |
| forEach? | No (single object) | Yes (iterable) |
| Use when | You need one element | You need a group |
How the Browser Searches
The browser walks the DOM tree in depth-first order (top to bottom, left to right). querySelector stops at the first match. querySelectorAll collects all matches into a NodeList.
How to Apply It
"#nav", ".card", "ul > li".const variable for reuse.Example 1: Select by ID
Given this HTML, select the main heading and change its text:
Example 2: Select by Class
Highlight every element with class "highlight":
Example 3: Complex Selectors
Select only the <a> tags inside a <nav> that have a specific data attribute:
Example 4: Scoped Selection
You can call querySelector on any element, not just document, to search only within that element's subtree:
NodeList vs HTMLCollection
Older methods like getElementsByClassName return an HTMLCollection, which behaves differently from the NodeList returned by querySelectorAll:
| NodeList | HTMLCollection | |
|---|---|---|
| From method | querySelectorAll | getElementsByClassName |
| forEach | ✓ Yes | ✕ No |
| Live updates? | Static (snapshot) | Live (auto-updates) |
| Selector power | Any CSS selector | Class or tag only |
Common Mistakes
Selection Methods Summary
| Method | Returns | Best For |
|---|---|---|
| querySelector | Element or null | Single, unique element |
| querySelectorAll | Static NodeList | Multiple elements |
| getElementById | Element or null | By ID (legacy) |
| getElementsByClassName | Live HTMLCollection | By class (legacy) |
| getElementsByTagName | Live HTMLCollection | By tag (legacy) |
Question 1 of 5
What does document.querySelector(".card") return if the page has three elements with the class "card"?
Question 2 of 5
Which selector string correctly targets an element with id="main-nav"?
Question 3 of 5
What happens when document.querySelector("#missing") finds no matching element on the page?
Question 4 of 5
You need to add an event listener to every <button> with class "delete-btn" on the page. Which approach is correct?
Question 5 of 5
What is the advantage of calling querySelector on a specific parent element instead of on document?