HTML DOM Document querySelectorAll()
Example
Select all elements with class="example":
document.querySelectorAll(".example");
Try it Yourself »
More examples below.
Definition and Usage
The querySelectorAll()
method returns all elements that matches a CSS selector(s).
The querySelectorAll()
method returns a NodeList.
The querySelectorAll()
method throws a SYNTAX_ERR exception if the selector(s) is invalid
Tutorials:
The JavaScript Node List Tutorial
QuerySelector Methods:
The Element querySelector() Method
The Element querySelectorAll() Method
The Document querySelector() Method
The Document querySelectorAll() Method
GetElement Methods:
The Document getElementById() Method
The Difference Between an HTMLCollection and a NodeList
A NodeList and an HTMLcollection is very much the same thing.
Both are array-like collections (lists) of nodes (elements) extracted from a document. The nodes can be accessed by index numbers. The index starts at 0.
Both have a length property that returns the number of elements in the list (collection).
An HTMLCollection is a collection of document elements.
A NodeList is a collection of document nodes (element nodes, attribute nodes, and text nodes).
HTMLCollection items can be accessed by their name, id, or index number.
NodeList items can only be accessed by their index number.
An HTMLCollection is always a live collection. Example: If you add a <li> element to a list in the DOM, the list in the HTMLCollection will also change.
A NodeList is most often a static collection. Example: If you add a <li> element to a list in the DOM, the list in NodeList will not change.
The getElementsByClassName()
and getElementsByTagName()
methods return a live HTMLCollection.
The querySelectorAll()
method returns a static NodeList.
The childNodes
property returns a live NodeList.
Syntax
document.querySelectorAll(CSS selectors)
Parameters
Parameter | Description |
CSS selectors | Required. One or more CSS selectors. CSS selectors select HTML elements based on id, classes, types, attributes, values of attributes etc. For a full list, go to our CSS Selectors Reference. For multiple selectors, separate each selector with a comma (See "More Examples"). |
Return Value
Type | Description |
Object | A NodeList object with the elements that matches the CSS selector(s). If no matches are found, an empty NodeList object is returned. |
More Examples
Add a background color to the first <p> element:
const nodeList= document.querySelectorAll("p");
nodeList[0].style.backgroundColor = "red";
Try it Yourself »
Add a background color to the first <p> element with class="example":
const nodeList = document.querySelectorAll("p.example");
nodeList[0].style.backgroundColor = "red";
Try it Yourself »
Number of elements with class="example":
let numb = document.querySelectorAll(".example").length;
Try it Yourself »
Set the background color of all elements with class="example":
const nodeList = document.querySelectorAll(".example");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.backgroundColor = "red";
}
Try it Yourself »
Set the background color of all <p> elements:
let nodeList = document.querySelectorAll("p");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.backgroundColor = "red";
}
Try it Yourself »
Set the border of all <a> elements with a "target" attribute:
const nodeList = document.querySelectorAll("a[target]");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.border = "10px solid red";
}
Try it Yourself »
Set the background color of every <p> element where the parent is a <div> element:
const nodeList = document.querySelectorAll("div > p");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.backgroundColor = "red";
}
Try it Yourself »
Set the background color of all <h3>, <div> and <span> elements:
const nodeList = document.querySelectorAll("h3, div, span");
for (let i = 0; i < nodeList.length; i++) {
nodeList[i].style.backgroundColor = "red";
}
Try it Yourself »
Browser Support
document.querySelectorAll()
is a DOM Level 3 (2004) feature.
It is fully supported in all browsers:
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |