Before a script can change an element, it must find it. The document object offers several methods for this, each suited to a different need.
Finding One Element
The method getElementById("title") returns the single element whose id is title, or null if none exists. The method querySelector(".note") accepts any CSS selector and returns the first element that matches it. Because it uses the same selectors as CSS, it is the most flexible method, and many developers use it for everything.
Finding Many Elements
The method getElementsByClassName("note") returns all elements with that class. The method querySelectorAll("p.note") returns every element that matches a CSS selector. Its result is a NodeList, a list-like collection that can be walked through with forEach.
Definition: NodeList. A list-like collection of nodes, returned by methods such as querySelectorAll.
Table 3.7: Methods for finding elements
| Method | Returns | Note |
|---|---|---|
| getElementById | One element | Fastest for an id |
| querySelector | First match | Any CSS selector |
| querySelectorAll | NodeList of all matches | Any CSS selector |
| getElementsByClassName | Live collection | By class name |
| getElementsByTagName | Live collection | By tag name |
Moving Around the Tree
Once an element is found, its relatives can be reached from it. parentElement gives the parent, children gives the child elements, and nextElementSibling and previousElementSibling give the neighbours. These properties let a script walk through the items of a list or move up from a clicked button to its card.
Checking Before Use
If a method finds nothing, it gives null, and any attempt to use null causes an error. A script should therefore check that an element exists, or make sure that it runs after the page has loaded.
Example 3.7: Highlighting selected elements
<h2 id="title">Notes</h2>
<p class="note">First note</p>
<p>Plain text</p>
<p class="note">Second note</p>
<script>
document.getElementById("title").style.color = "#1F3864";
const notes = document.querySelectorAll(".note");
notes.forEach(n => n.style.background = "#FFF2CC");
console.log(notes.length);
</script>
Output in the browser:
Both paragraphs with the class note receive a yellow background, and the plain paragraph is unchanged.
Selecting in Practice
A script usually selects once and keeps the result in a variable, instead of searching the page again and again. Searching inside an element is faster and clearer than searching the whole document: card.querySelector(".title") looks only within one card. For a list of items, querySelectorAll("li") followed by forEach handles each item in turn. When elements are added later by a script, they are not part of an earlier NodeList, so the selection must be repeated.
Common mistakes
- Running the script before the elements exist, so the selector finds nothing.
- Forgetting the dot or hash in a selector, such as querySelector("note").
- Using the result of querySelector without checking for null.
- Using a single-element method when several elements must change.
Key takeaways
- Use getElementById for one element by id, and querySelector for the first CSS match.
- querySelectorAll returns a NodeList that can be looped with forEach.
- parentElement, children and sibling properties move through the tree.
- A missing element gives null, so check before using it.
Practice questions
- What does querySelector return if nothing matches?
- Which method returns all elements that match a CSS selector?
- Which property gives the parent of an element?
Answers to the practice questions
- null.
- querySelectorAll.
- parentElement.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III
