document.querySelectorAll() is an instance method that returns a static NodeList of every element matching a CSS selector string (see MDN Document: querySelectorAll()). Learn multiple selectors, attribute lookups, forEach loops, SyntaxError / CSS.escape() handling, how it compares to querySelector() and live collections, and five try-it labs.
01
Kind
Instance method
02
Arg
CSS selectors
03
Returns
static NodeList
04
Match
All matches
05
Throws
SyntaxError
06
Status
Baseline
Fundamentals
Introduction
Where querySelector stops at the first hit, querySelectorAll collects every match into a list you can loop. Pass the same CSS selector language you already know from stylesheets.
MDN: the result is a static (not live)NodeList in document order — parents before children, earlier siblings before later ones. No matches means an empty list (not null). If the selector includes a CSS pseudo-element, the list is always empty.
💡
Think: “CSS find — every hit”
1) Write a valid CSS selector (e.g. "p" or ".note, .alert") 2) Call document.querySelectorAll(selector) 3) Check length or loop with forEach 4) Remember the list does not update if the DOM changes later
Return value — static NodeList of matching Elements, or empty if none (MDN).
Order — document order (MDN).
Static — not live; later DOM changes do not update the list (MDN).
Pseudo-elements — always yield an empty list (MDN).
Escape special ids — use CSS.escape() when needed (MDN).
Foundation
📝 Syntax
General form of Document.querySelectorAll (MDN):
JavaScript
querySelectorAll(selectors)
Parameters
selectors — a string containing one or more selectors to match. Must be valid CSS; otherwise a SyntaxError is thrown (MDN). Escape class/id values that are not valid CSS identifiers with CSS.escape() (MDN).
Return value
A non-live NodeList with one Element per match, or an empty NodeList when nothing matches (MDN). Elements are in document order (MDN).
Exceptions
SyntaxErrorDOMException — thrown if the selector syntax is invalid (MDN).
processed: 3
(each .highlighted gains class "processed")
How It Works
MDN notes you can examine the list like an array. Modern browsers support NodeList.prototype.forEach.
Example 5 — querySelectorAll vs querySelector
Same selector, different return shapes.
JavaScript
const first = document.querySelector(".card");
const all = document.querySelectorAll(".card");
console.log("first tag:", first ? first.tagName : "null");
console.log("all length:", all.length);
console.log("same first node:", first === all[0]);
Document.querySelectorAll() is Baseline Widely available on MDN (across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.
✓ Baseline Widely available
Document.querySelectorAll()
CSS-powered all-match DOM lookup — static NodeList across all major browsers.
BaselineWidely available
Google Chrome1+
Yes
Mozilla Firefox3.5+
Yes
Apple Safari3.1+
Yes
Microsoft Edge12+
Yes
Opera10+
Yes
Internet Explorer9+
Yes
querySelectorAll()Wide
Bottom line: Use querySelectorAll when you need every match. Prefer querySelector for a single node, and remember the NodeList is static.
Wrap Up
Conclusion
document.querySelectorAll(selectors) is the everyday way to collect every element that matches a CSS selector into a static NodeList. Loop with forEach, check length when needed, escape special ids, and fall back to querySelector when one node is enough.
Call again after DOM changes if you need a fresh snapshot
Escape dynamic special ids with CSS.escape() (MDN)
❌ Don’t
Assume the list updates live like getElementsByClassName
Pass invalid CSS without try/catch
Expect CSS pseudo-elements to return nodes (MDN)
Confuse empty list with null
Use a huge document-wide scan when a scoped element search is enough
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about querySelectorAll()
CSS-powered all-match lookup — static NodeList.
5
Core concepts
📝01
Returns
NodeList
static
🗂02
Match
all matches
CSS
⚠️03
Invalid
SyntaxError
MDN
📋04
No match
empty list
length 0
🛡05
Status
Baseline
2015
❓ Frequently Asked Questions
MDN: Document.querySelectorAll() returns a static (not live) NodeList of the document’s elements that match the specified group of CSS selectors.
No. MDN marks Document.querySelectorAll() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
You get an empty NodeList (length 0), not null (MDN). Still safe to call forEach — it simply runs zero times.
MDN: it is static (non-live). It does not automatically update when the DOM changes after the call.
querySelector() returns the first matching Element or null. querySelectorAll() returns every match in a NodeList (empty when none).
A SyntaxError DOMException is thrown if selectors is not a valid CSS selector string (MDN). Escape special class/id values with CSS.escape().
Did you know?
Because the NodeList is static, you can safely remove or move nodes while looping without the list shifting under you mid-iteration — a common gotcha with live HTMLCollection APIs like getElementsByClassName().