querySelector
Returns the first matching element.
Selecting elements is how JavaScript finds the part of the page it needs to read or change.
const panel = document.querySelector("[data-panel]");
const buttons = panel.querySelectorAll("button");
buttons.forEach(button => button.disabled = false);
DOM & HTML Interaction
The most common selector methods are querySelector and querySelectorAll. They accept CSS selectors, which makes them flexible and familiar.
Selecting from document searches the whole page. Selecting from a container searches only inside that container, which reduces accidental matches.
A selector is a contract between HTML and JavaScript. If the class or attribute changes, the JavaScript may stop finding the element.
Returns the first matching element.
Returns all matching elements as a static NodeList.
Fast direct lookup by id.
Search from a known parent instead of the entire document.
Examples
const panel = document.querySelector("[data-panel]");
const button = panel.querySelector("[data-submit]");
const button = document.querySelector("button");
// This might select the first button on the page, not your button.
Code patterns
These patterns are the DOM moves you use constantly: select, read, change, create, navigate and render.
Use data attributes for JavaScript behavior.
const saveButton = document.querySelector("[data-save]");
Find children inside a component root.
const panel = document.querySelector("[data-panel]");
const title = panel.querySelector("[data-title]");
NodeList supports forEach in modern browsers.
document.querySelectorAll("[data-tab]").forEach(tab => {
tab.hidden = false;
});
Use Array.from when you need array methods.
const items = Array.from(document.querySelectorAll("li"));
const visible = items.filter(item => !item.hidden);
Rules that matter
DOM APIs are powerful because they affect the live page. Use them with stable hooks, clear state and safe text updates.
Styling classes can change during redesigns.
Start from the closest stable container.
button, div and .active are often too broad.
querySelectorAll length reveals selector mistakes.
IDs must be unique and can make reuse harder.
data-submit is clearer than data-x.
Production thinking
A weak selector creates invisible bugs: the code still runs, but it changes the wrong element.
Selectors should not depend on accessible text that may be translated or changed for clarity.
Production UI code should use stable hooks and scoped roots so pages can grow safely.
Reliable selectors keep rendered navigation, headings and structured content from breaking during client-side updates.
Live code lab
The preview runs inside an isolated iframe. The JavaScript is placed inside the HTML editor for now, so every example stays together and remains easy to understand.
Mini assignment
Practice assignment
Try it yourself
Self-check
If you can explain the DOM operation and its failure path, you are ready to use it in real UI code.
Senior audit upgrade
Prefer textContent, createElement, templates and replaceChildren for normal UI rendering. Reach for innerHTML only when the markup is controlled and the risk is understood.
const item = document.createElement("li");
item.textContent = userInput;
list.replaceChildren(item);