Syntax
document.getElementById(id)
document.querySelector(selector)
document.querySelectorAll(selector)Examples
getElementById() and querySelector()
Finding a single element two different ways.
const byId = document.getElementById("header");
const bySelector = document.querySelector("#header"); // equivalent, but more flexible
const firstButton = document.querySelector("button");
const specificClass = document.querySelector(".btn-primary");
const nested = document.querySelector("nav .menu-item.active");querySelectorAll() and Looping
Selecting multiple elements and iterating over them.
const items = document.querySelectorAll(".list-item");
console.log(items.length); // number of matches
items.forEach(item => {
console.log(item.textContent);
});
// NodeList supports forEach directly, but is NOT a real array
// use Array.from(items) if you need map/filter/etc.Best practices
- Use querySelector()/querySelectorAll() for their flexibility with any valid CSS selector, rather than mixing several older, more specific methods
- Use getElementById() when selecting by a known unique ID - it is marginally faster and communicates clear intent
- Remember querySelectorAll() returns a static NodeList - it does not update automatically if the DOM changes afterward
- Convert a NodeList to a real array with Array.from() if you need array methods like map() or filter() on the results
At a glance
- Purpose
- Application logic and interaction
- File extension
- .js ยท .mjs
- Runs in
- Browsers and JavaScript runtimes
- Usually used with
- HTML, CSS and Web APIs
Specifications & further reading
Related JavaScript documentation
Creating and Modifying Elements
createElement() builds a new DOM element in memory, which can then be customized and inserted into the page with methods like appendChild() or append(). textContent sets plain text safely, while innerHTML parses and inserts HTML markup - which carries injection risks if the content includes untrusted user input.classList: add, remove, toggle, contains
The classList property provides a convenient API for managing an element's CSS classes without manually parsing the className string. add() and remove() add or remove one or more classes, toggle() switches a class on or off based on its current presence, and contains() checks whether a class is currently applied.IntersectionObserver
IntersectionObserver efficiently detects when an element enters or exits the viewport (or another container), without the performance cost of manually listening to scroll events and calculating positions. It is the standard tool for lazy-loading images, infinite scroll, and scroll-triggered animations.MutationObserver
MutationObserver watches for changes to the DOM tree - added/removed elements, attribute changes, or text content changes - and runs a callback in response. It is useful for reacting to DOM changes made by other scripts or third-party widgets that you do not directly control.
createElement() builds a new DOM element in memory, which can then be customized and inserted into the page with methods like appendChild() or append(). textContent sets plain text safely, while innerHTML parses and inserts HTML markup - which carries injection risks if the content includes untrusted user input.classList: add, remove, toggle, contains
The classList property provides a convenient API for managing an element's CSS classes without manually parsing the className string. add() and remove() add or remove one or more classes, toggle() switches a class on or off based on its current presence, and contains() checks whether a class is currently applied.IntersectionObserver
IntersectionObserver efficiently detects when an element enters or exits the viewport (or another container), without the performance cost of manually listening to scroll events and calculating positions. It is the standard tool for lazy-loading images, infinite scroll, and scroll-triggered animations.MutationObserver
MutationObserver watches for changes to the DOM tree - added/removed elements, attribute changes, or text content changes - and runs a callback in response. It is useful for reacting to DOM changes made by other scripts or third-party widgets that you do not directly control.