The DOM Lesson 22 of 30
The JavaScript HTML DOM — Finding Elements
The DOM is how JavaScript sees your HTML page. Find elements with getElementById, querySelector and querySelectorAll, and understand the page as a tree.
When a browser loads your HTML, it builds a model of the page in memory called the DOM (Document Object Model). JavaScript can't change your HTML file, but it can change the DOM, and whatever changes there shows on the screen instantly.
The page as a tree
The DOM is shaped like a family tree. Every element is a node, and elements inside other elements are their children:
document
└── html
├── head
│ └── title
└── body
├── h1
├── p
└── ul
├── li
└── li
The document object is the root of everything. Every DOM task starts with it.
Finding one element
getElementById
The fastest and most common way, when the element has an id:
<h2 id="title">Grade 10 Class List</h2>
<script>
const title = document.getElementById("title");
title.style.color = "#0f5c4d";
title.textContent += " ✔";
</script>
querySelector
querySelector uses CSS selectors, the same ones you learned in the CSS course. It returns the first match:
<p class="note">First note</p>
<p class="note">Second note</p>
<ul id="menu"><li>Home</li><li>Lessons</li></ul>
<script>
document.querySelector(".note").style.background = "#fdf3dc"; // first .note
document.querySelector("#menu li:last-child").style.fontWeight = "bold";
</script>
Finding many elements
querySelectorAll returns all matches as a list (a NodeList). Loop through it with forEach or for...of:
<ul>
<li class="grade">88</li>
<li class="grade">72</li>
<li class="grade">95</li>
<li class="grade">69</li>
</ul>
<script>
const items = document.querySelectorAll(".grade");
items.forEach((li) => {
const g = Number(li.textContent);
li.style.color = g >= 75 ? "green" : "crimson";
li.textContent += g >= 75 ? " – PASSED" : " – FAILED";
});
</script>
Selector cheat sheet
| Method | Returns | Example |
|---|---|---|
getElementById("x") | One element or null | getElementById("total") |
querySelector(css) | First match or null | querySelector(".card h3") |
querySelectorAll(css) | List of all matches | querySelectorAll("li") |
You may also see older methods like getElementsByClassName and getElementsByTagName. They still work, but querySelector and querySelectorAll can do everything they do.
When nothing is found
If no element matches, getElementById and querySelector return null. Using null as if it were an element causes the famous error:
TypeError: Cannot set properties of null (setting 'textContent')
Searching inside an element
You can call querySelector on any element, not just document, to search only inside it:
const card = document.querySelector("#student-card");
const name = card.querySelector(".name"); // only looks inside the card
Moving around the tree
From one element, you can reach its relatives:
el.parentElement // the element it's inside
el.children // its child elements
el.firstElementChild // first child
el.nextElementSibling // the next element at the same level
el.closest(".card") // nearest ancestor matching a selector
closest is especially handy with buttons inside cards: from the clicked button, jump up to the card it belongs to.
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04