English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

HTML DOM querySelectorAll() method

HTML DOM Document Object

querySelectorAll()Returns a list of elements in the document that match the specified selector group (uses depth-first pre-order traversal of the document's nodes). The returned object is NodeList .

The elements in the returned NodeList are sorted in the order they appear in the source code.

The returned NodeList can be accessed by index. The index starts from 0.

NodeList is a static collection, which means changes in the DOM are not effective on this collection.

Use the length property of NodeList to determine the number of elements with the specified selector, and then you can iterate over all elements to extract the required information.

Syntax:

document.querySelectorAll(selectors)
document.querySelectorAll("div");
Test and See‹/›

Browser compatibility

The numbers in the table specify the first browser version that fully supports the querySelectorAll() method:

Methods
querySelectorAll()13.5103.28

Parameter value

ParameterDescription
selectorsA string containing one or more selector strings to match. This string must be validCSS SelectorString.

Technical details

Return value:A NodeList object representing the nodes in the document that match the specifiedCSS SelectorAll matching elements.
Exception cases:SyntaxError-Invalid syntax for the specified selector string
DOM version:DOM Levels1

More Examples

Get all elements with class="demo":

document.querySelectorAll(".demo");
Test and See‹/›

This example returns a list of all <div> elements in the document with the class 'note':

document.querySelectorAll("div.note");
Test and See‹/›

Set the background color of all <h1>, <p> and <div> element background color:

document.querySelectorAll("h1, p, div");
Test and See‹/›

Related References

CSS Tutorial:CSS Selector

CSS Reference:CSS #idSelector

CSS Reference: CSS .class Selector

DOM Document querySelector() Methods

DOM Document getElementsByClassName() Methods

DOM Document getElementsByTagName() Methods

HTML DOM Document Object