JavaScript Element querySelectorAll() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline
Instance method

What You’ll Learn

Element.querySelectorAll() is an instance method that returns a static NodeList of every descendant matching a CSS selector—in document order. Learn MDN list patterns, forEach loops, :scope scoping, attribute selectors, Array.from(), and five try-it labs aligned with MDN.

01

Kind

Instance method

02

Finds

All descendants

03

Arg

CSS selector string

04

Returns

Static NodeList

05

Empty

length 0

06

Status

Baseline widely

Introduction

Sometimes you need every match—all highlighted list items, every input in a form, or each card with a certain class. querySelector() only gives you the first result; querySelectorAll() collects them all.

querySelectorAll(selectors) returns a static (non-live) NodeList of matching descendant elements (MDN). Results stay in document order: parents before children, earlier siblings before later ones.

💡
Beginner tip

No matches returns an empty NodeList (length === 0), not null. Check list.length before looping. For array methods like map, use Array.from(list) (MDN).

This page is part of JavaScript Element. Pair with querySelector() when you only need the first match, and matches() to test a single element.

Understanding the querySelectorAll() Method

Calling container.querySelectorAll(selectors) searches descendants of container and returns every match in a NodeList.

  • It is an instance method on Element (MDN).
  • Returns a static NodeList—it does not update when the DOM changes (MDN).
  • Elements are in document order (MDN).
  • Empty NodeList when nothing matches—never null (MDN).
  • MDN: selectors apply to the whole document; use :scope at the start to restrict scope.
  • MDN: if selectors include a CSS pseudo-element, the returned list is always empty.
  • Throws SyntaxError if selectors is not valid CSS (MDN).

📝 Syntax

General form of Element.querySelectorAll (MDN):

JavaScript
querySelectorAll(selectors)

Parameters

NameTypeDescription
selectorsStringA string containing one or more selectors to match. Must be a valid CSS selector string (MDN). Prefix with :scope to limit matching to the caller.

Return value

TypeDescription
NodeListA static NodeList of matching descendant Element objects in document order, or an empty NodeList when nothing matches (MDN).

Exceptions

  • SyntaxError DOMException — thrown if selectors is not a valid CSS selector string (MDN).

Common patterns

JavaScript
// All paragraphs inside myBox (MDN)
const matches = myBox.querySelectorAll("p");

// Multiple selectors (MDN)
const notes = myBox.querySelectorAll("div.note, div.alert");

// Attribute selector in a container (MDN)
const active = container.querySelectorAll("li[data-active='1']");

// Loop with forEach (MDN)
highlightedItems.forEach((item) => item.classList.remove("highlighted"));

// Convert to array for map/filter
const arr = Array.from(list);

// Scope to caller with :scope (MDN)
subject.querySelectorAll(":scope #inner");

⚡ Quick Reference

GoalCode
All matches in containerel.querySelectorAll(".item")
Multiple selectorsbox.querySelectorAll("div.note, div.alert")
Attribute matchul.querySelectorAll("li[data-active='1']")
Scope to callerel.querySelectorAll(":scope > li")
No matchempty NodeList (length === 0)
MDN statusBaseline Widely available (since July 2015)

🔍 At a Glance

Four facts to remember about Element.querySelectorAll().

Returns
NodeList

Static list

Baseline
widely

Since July 2015

Empty
length 0

Not null

Order
document

Tree order

📋 querySelectorAll() vs querySelector() vs getElementsByClassName()

querySelectorAll()querySelector()getElementsByClassName()
MatchesAll descendantsFirst onlyBy class name
Return valueStatic NodeListElement or nullLive HTMLCollection
No matchEmpty NodeListnullEmpty collection
Selector powerFull CSS selectorsFull CSS selectorsClass string only
Best forEvery match in subtreeOne targetLegacy class lists

Examples Gallery

Examples follow MDN Element.querySelectorAll() patterns. Use View Output or Try It Yourself for each case.

📚 Getting Started

MDN patterns for collecting every match inside a container.

Example 1 — All <p> Inside a Box (MDN)

Return every paragraph descendant of myBox.

JavaScript
const myBox = document.getElementById("myBox");
const matches = myBox.querySelectorAll("p");

console.log(matches.length); // 3
console.log(matches[0].textContent); // "First"
Try It Yourself

How It Works

MDN: myBox.querySelectorAll("p") returns a NodeList of every p inside myBox, in document order.

Example 2 — Multiple Selectors (MDN)

Match elements with class note or alert.

JavaScript
const myBox = document.getElementById("myBox");
const matches = myBox.querySelectorAll("div.note, div.alert");

console.log(matches.length); // 2
Try It Yourself

How It Works

MDN: comma-separated selectors work like CSS—any element matching at least one selector is included in the NodeList.

📈 Practical Patterns

Attribute filters, loops, and :scope scoping.

Example 3 — Attribute Selector (MDN)

Find list items with data-active="1" inside a container.

JavaScript
const container = document.querySelector("#user-list");
const matches = container.querySelectorAll("li[data-active='1']");

console.log(matches.length);
matches.forEach((li) => console.log(li.textContent));
Try It Yourself

How It Works

MDN uses attribute selectors to narrow results inside a scoped container such as #user-list.

Example 4 — Loop with forEach (MDN)

Remove a class from every highlighted item in a list.

JavaScript
const userList = document.getElementById("user-list");
const highlightedItems = userList.querySelectorAll(".highlighted");

highlightedItems.forEach((userItem) => {
  userItem.classList.remove("highlighted");
});

console.log(userList.querySelectorAll(".highlighted").length); // 0
Try It Yourself

How It Works

MDN: NodeLists support forEach directly. For map or filter, convert with Array.from(nodeList).

Example 5 — Selector Scope with :scope (MDN)

Compare document-wide vs scoped selector matching on #subject.

JavaScript
const subject = document.querySelector("#subject");

const withoutScope = subject.querySelectorAll("#outer #inner");
const withScope = subject.querySelectorAll(":scope #outer #inner");

console.log(withoutScope.length); // 1 (MDN demo)
console.log(withScope.length);    // 0
Try It Yourself

How It Works

MDN: without :scope, selectors apply to the whole document. Prefixing with :scope restricts matching to the element you called the method on.

🚀 Common Use Cases

  • Collecting every highlighted row in a table or list (MDN forEach pattern).
  • Batch-updating all elements with a class inside a component.
  • Attribute filters: container.querySelectorAll("[data-name*='funnel']") (MDN).
  • Form validation across all invalid inputs in a section.
  • Scoped queries with :scope when document-wide matching is too broad (MDN).
  • Converting results to arrays: Array.from(nodeList) for map/filter.

🧠 How querySelectorAll() Builds a NodeList

1

Call on a base element

base.querySelectorAll(selectors) — pass a CSS selector string (MDN).

Start
2

Match and filter

Browser finds matches (document-wide unless :scope), keeps descendants of base (MDN).

Match
3

Collect in document order

All qualifying elements are added to a static NodeList.

List
4

Return NodeList

Use length, indexing, or forEach—empty when no matches (MDN).

📝 Notes

  • Not Deprecated, Experimental, or Non-standard on MDN (Baseline Widely available, July 2015).
  • Returns a static NodeList—not live (MDN).
  • Empty NodeList when nothing matches—check length, not null.
  • MDN: pseudo-element selectors always return an empty list.
  • Use querySelector() when you only need the first match.
  • Related: querySelector(), matches(), closest(), JavaScript hub.

Browser Support

Element.querySelectorAll() is Baseline Widely available (MDN: across browsers since July 2015). Logos use the shared browser-image-sprite.png sprite from this project.

Baseline Widely available

Element.querySelectorAll()

Get all matching descendants as a static NodeList — in document order.

Baseline Widely available
Google Chrome Supported · Desktop & Mobile
Yes
Microsoft Edge Supported · Chromium
Yes
Mozilla Firefox Supported · Desktop & Mobile
Yes
Apple Safari Supported · macOS & iOS
Yes
Opera Supported · Modern versions
Yes
Internet Explorer 8+ · IE8 with limitations
Yes
querySelectorAll() Excellent

Bottom line: Use querySelectorAll() when you need every match inside an element. Pair with forEach or Array.from() for batch updates.

Conclusion

Element.querySelectorAll() collects every matching descendant in a static NodeList. Loop with forEach, check length for empty results, and prefix with :scope when you need true scoped matching.

Continue with querySelector(), matches(), releasePointerCapture(), shadowRoot, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Check list.length before assuming matches exist
  • Use forEach for batch DOM updates (MDN)
  • Convert with Array.from() when you need array methods
  • Prefix with :scope to limit selector scope (MDN)
  • Escape dynamic ids with CSS.escape()

❌ Don’t

  • Expect null—no match means empty NodeList
  • Assume the NodeList stays in sync with DOM changes (it is static)
  • Use querySelectorAll when one match is enough
  • Call map directly on NodeList without converting
  • Include pseudo-element selectors if you expect results (always empty per MDN)

Key Takeaways

Knowledge Unlocked

Five things to remember about Element.querySelectorAll()

Every matching descendant in document order.

5
Core concepts
📄 02

Empty

length 0

Not null
✍️ 03

Loop

forEach

MDN
04

Baseline

since 2015

Status
05

Scope

:scope

MDN

❓ Frequently Asked Questions

It returns a static NodeList of all descendant elements that match the specified CSS selectors (MDN).
No. MDN marks Element.querySelectorAll() as Baseline Widely available (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.
A static (non-live) NodeList of matching Element objects in document order, or an empty NodeList when nothing matches (MDN).
querySelectorAll() returns every match in a NodeList. querySelector() returns only the first match as an Element or null.
MDN: querySelectorAll() returns a static NodeList — it does not update when the DOM changes after the call.
A SyntaxError DOMException is thrown if selectors is not a valid CSS selector string (MDN).
Did you know?

MDN notes that querySelectorAll() returns a static NodeList—unlike older live collections from getElementsByClassName, it does not auto-update when the DOM changes after your call.

More Element Topics

Browse Element methods and properties to keep building your DOM skills.

releasePointerCapture() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful