.querySelectorAll()
Anonymous contributor
Published Mar 12, 2025
Contribute to Docs
In JavaScript, the .querySelectorAll()
method under the document
object returns a static (not live) NodeList
of all elements that match the given group of selectors.
Syntax
document.querySelectorAll(selectors);
selectors
: Represents a string containing one or more CSS selectors used to match elements in the document. It follows the same rules as CSS selectors and can include:- Type selectors (
div
,p
,span
) - Class selectors (
.class-name
) - ID selectors (
#id-name
) - Attribute selectors (
[type="text"]
,[disabled]
) - Combinations (
div p
,.container > p
,ul > li:first-child
)
- Type selectors (
Examples
Example 1
In this example, a NodeList
of all <p>
elements in the document is obtained:
const matches = document.querySelectorAll('p');
Example 2
The following example returns a list of all <div>
elements in the document with a class of either note
or alert
:
const matches = document.querySelectorAll('div.note, div.alert');
Example 3
In this example, a list of <p>
elements is obtained, whose immediate parent is a <div>
with the class highlighted
, and which are inside a container with the ID test
:
const container = document.querySelector('#test');const matches = container.querySelectorAll('div.highlighted > p');
All contributors
- Anonymous contributor
Contribute to Docs
- Learn more about how to get involved.
- Edit this page on GitHub to fix an error or make an improvement.
- Submit feedback to let us know how we can improve Docs.
Learn JavaScript on Codecademy
- Career path
Front-End Engineer
Front-end engineers work closely with designers to make websites beautiful, functional, and fast.Includes 34 CoursesWith Professional CertificationBeginner Friendly115 hours - Free course
Learn JavaScript
Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.Beginner Friendly15 hours