Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

Element.querySelectorAll — finding descendant elements

Element.querySelectorAll returns a static NodeList of descendants matching a CSS selector. Later tree changes do not update the list.

Version: ECMAScript and Web APIs; exact feature edition and browser support reviewed per entry; Node 24 in repository does not prove browser API support

Syntax

element.querySelectorAll(selectors)

Element.querySelectorAll(selectors) returns matching descendants in tree order as a NodeList, not an array. The calling element itself is excluded. No matches means an empty list; querySelector instead returns the first element or null.

The list is static: tree changes do not update its membership, but its elements are not copies. Calling again gives current results. An invalid selector throws a DOMException named SyntaxError.

A selector can consider ancestors outside the calling element; use :scope > .item to target direct children. This is a DOM API, not an ECMAScript feature; Node.js availability alone does not establish support.

Examples

const root = document.createElement('section');
const first = document.createElement('span');
first.className = 'item';
root.append(first);

const matches = root.querySelectorAll('.item');
const second = document.createElement('span');
second.className = 'item';
root.append(second);

console.log(matches.length);
console.log(root.querySelectorAll('.item').length);
console.log(matches[0] === first);

Result

1
2
true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources