HTML DOM querySelectorAll()Method
Examples
Get all elements with class="example" in the document:
Try it Yourself »
Definition and Usage
The querySelectorAll() method returns all elements in the document that match the specified CSS selector(s), and returns aNodeListNodeList object.
The NodeList object represents a collection of nodes. It can be accessed by index, with index values starting from 0.
Tip:You can use the NodeList object's lengthproperty to get the attributes of elements matching the selector, and then you can iterate through all elements to obtain the information you want.
For more CSS selectors, please refer to theCSS Selectors Tutorial , CSS Selectors Reference Manual。
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
| Method | |||||
|---|---|---|---|---|---|
| querySelectorAll() | 4.0 | 8.0 | 3.5 | 3.2 | 10.0 |
Note:Internet Explorer 8 supports CSS2 selectors. IE9 and later browsers support CSS3 selectors.
Syntax
elementList = document.querySelectorAll(selectors);
- elementList is a static NodeList object.
- selectors is a string containing one or more CSS selectors separated by commas.
Parameter Values
| Parameter | Type | Description |
|---|---|---|
| CSS selector | String | Required. Specifies one or more elements that match the CSS selector(s). Elements can be obtained using id, class, type, attribute, attribute value, etc. as selectors. Multiple selectors are separated by commas (,). Tip:For more content on CSS selectors, please refer to theCSS Selectors Reference Manual。 |
Method
| DOM Version: | Level 1 Document Object |
|---|---|
| Return Value: | A NodeList object representing all elements in the document that match the specified CSS selector(s). NodeList is a static NodeList object. If the specified selector is invalid, a SYNTAX_ERR exception is thrown. |
More Examples
Example
Get all <p> elements in the document, and set the background color for the first matching <p> element (index 0):
Try it Yourself »
Example
Get all <p> elements with class="example" in the document, and set the background color for the first matching <p> element (index 0):
Try it Yourself »
Example
Count the number of <p> elements with class="example" in the document (using the length property of the NodeList object):
Try it Yourself »
Example
Set the background color of all elements with class="example" in the document:
Try it Yourself »
Example
Set the background color of all <p> elements in the document:
Try it Yourself »
Example
Find all <a> tags in the document that contain the "target" attribute, and set a border for them:
Try it Yourself »
Example
Find each <p> element whose parent element is <div>, and set the background color for them:
Try it Yourself »
Example
Set the background color for all <h2>, <div>, and <span> elements in the document:
Try it Yourself »
Related Articles
CSS Tutorial:CSS Selectors
CSS Reference Manual:CSS Selectors Reference Manual
JavaScript Tutorial:JavaScript HTML DOM Node Lists
HTML DOM Reference:document.querySelector()
HTML DOM Reference:element.querySelectorAll()
Other Extensions
Document Object