Learning objective: Select elements with getElementById, querySelector and querySelectorAll using CSS selectors, handle missing elements, and search inside a container.
const banner = document.querySelector("#banner");
console.log(banner);
console.log(banner?.textContent ?? "no banner on this page");
console.log(document.querySelectorAll(".none").length);
Sample input
None
Expected output
null
no banner on this page
0
Common mistake
Calling banner.textContent without checking, which throws a TypeError.
A sale banner applies a discount to every price on a product page. The script selects all price elements with one selector, reads each value and writes the new price back, so adding a product to the page needs no script change.
NodeList has no map; spread it into an array first
There are no buttons
map needs two arguments
querySelectorAll returns null
4. For two .card elements each with one .price inside, what is printed?
const first = document.querySelector(".card");
console.log(first.querySelectorAll(".price").length);
1
2
0
An error
5. Which selector matches buttons with a data-action attribute?
button.data-action
button[data-action]
button#data-action
data-action button
6. Which is the more stable hook for a script to find a delete button?
button.red.small
[data-action="delete"]
div > div > button:nth-child(3)
button:last-child
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
Fix the selector: Prints the total
Sum the prices: Prints Sum: 35; Uses querySelectorAll
Query helpers: Prints texts and count; Scoped texts; Missing section
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-11-the-document-object-model/lesson-052-selecting-elements/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
getElementById takes a bare id; querySelector takes any CSS selector.
querySelector returns the first match or null.
querySelectorAll returns a NodeList of all matches.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationselecting DOM elements
Context: I am learning selecting DOM elements in JavaScript.
Task: Explain getElementById, querySelector and querySelectorAll with id, class and attribute selectors.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionselecting DOM elements drill
Context: I have practised selecting DOM elements.
Task: Give me six HTML snippets with selector code and ask what each prints, including null cases.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingselecting DOM elements bug
Context: My querySelector call returns null even though the element is on the page.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationselecting DOM elements practice
Context: I finished the selecting DOM elements exercises.
Task: Create three exercises that select and summarise elements on a product page.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewselecting DOM elements review
Context: Here is my code that uses selecting DOM elements.
Task: Review my selectors for fragility and missing-element handling.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.