Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / JavaScript

document.querySelectorAll — all matching elements

Finds elements using CSS selectors and returns a static NodeList, including an empty list when nothing matches.

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

document.querySelectorAll(selectors)

document.querySelectorAll(selectors) returns a static NodeList of matching elements in document tree order. The argument is a CSS selector string, including a comma-separated list.

No matches means an empty list, not null. An invalid selector throws a DOMException named SyntaxError. In contrast, document.querySelector(selectors) returns the first matching element or null.

A static list retains its membership after DOM changes; it does not copy elements. Query again for updated results. The example uses a separate document, leaving the open page unchanged.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Document.querySelectorAll</title>
</head>
<body>
  <h1>All matching elements</h1>
  <ul id="items">
    <li class="item">First item</li>
  </ul>
  <script>
    const matches = document.querySelectorAll('.item');
    const added = document.createElement('li');
    added.className = 'item';
    added.textContent = 'Second item';
    document.getElementById('items').append(added);

    console.log(matches.length);
    console.log(document.querySelectorAll('.item').length);
    console.log(document.querySelectorAll('.missing').length);
  </script>
</body>
</html>

Result

1
2
0

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