JavaScript Element querySelectorAll() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Method

Definition

The Element.querySelectorAll() method returns a static NodeList of all descendant elements that match a CSS selector (MDN). No matches → an empty list (not null).

Pass the required arguments shown below. Check return values and browser support before you rely on the result.

Syntax

Call el.querySelectorAll. Start with this line.

JavaScript
el.querySelectorAll(selectors)
CallWhat it does
el.querySelectorAll(selectors)Primary signature.
el.querySelectorAll(...)Call with the arguments your case needs.

Built into the browser. Call querySelectorAll(...) on an Element — no import required.

Library declarations
JavaScript
el.querySelectorAll(selectors)

Parameters

ParameterDescription
Parent elementRequired. Results are descendants of this node
selectorsRequired. Valid CSS selector string

Return Value

Return ValueDescription
*Static NodeList (may be empty)

JavaScript Examples

Example 1 — Collect Matches (MDN)

Get every .item under a list and report the count.

JavaScript
const list = document.getElementById("list");
const out = document.getElementById("out");

const matches = list.querySelectorAll(".item");
out.textContent = "count: " + matches.length;
Try It Yourself

How It Works

  1. Scope to a parent. Search only under #list.
  2. Pass a CSS selector. ".item" is valid CSS.
  3. Get a NodeList. Every match is included.
  4. Read length — 0 means no matches.

Example 2 — Loop With forEach (MDN)

Walk each match and build a simple report.

JavaScript
const list = document.getElementById("list");
const out = document.getElementById("out");
const lines = [];

list.querySelectorAll(".item").forEach((item) => {
  lines.push(item.textContent);
});
out.textContent = lines.join(", ");
Try It Yourself

How It Works

  1. Collect the NodeList with querySelectorAll.
  2. Call forEach — MDN’s common looping pattern.
  3. Read each node (here, textContent).
  4. Need map/filter? Convert with Array.from(list) first.

Example 3 — Direct Children With :scope

Count only direct child spans under the parent.

JavaScript
const parent = document.getElementById("parent");
const out = document.getElementById("out");

const kids = parent.querySelectorAll(":scope > span");
out.textContent = "direct spans: " + kids.length;
Try It Yourself

How It Works

  1. Start from the parent. MDN uses :scope for the calling element.
  2. Require direct children with the > combinator.
  3. Nested spans are excluded from this NodeList.
  4. Length tells you how many direct matches exist.

Browser Support

Element.querySelectorAll() is Baseline Widely available on MDN (across browsers since July 2015). It is not Deprecated, Experimental, or Non-standard.

✓ Baseline Widely available

Element.querySelectorAll()

Collect all matching descendants as a static NodeList. Widely available since 2015.

2015+ Widely available
Google Chrome Yes
Supported
Mozilla Firefox Yes
Supported
Apple Safari Yes
Supported
Microsoft Edge Yes
Supported
Opera Yes
Supported
Internet Explorer IE8+
Partial
querySelectorAll() Baseline

Bottom line: Safe for modern browsers. Prefer querySelector when you only need the first match. See the MDN querySelectorAll() reference.

Frequently Asked Questions

MDN: it returns a static NodeList of all descendant elements that match the specified CSS selectors.
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).

Next: releasePointerCapture()

Continue with the Element releasePointerCapture() method.

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.

14 people found this page helpful