HTML DOM querySelectorAll() method
Example
Get all elements with class="example" in the document:
var
x
=
document
.
querySelectorAll
(
"
.example
"
)
;
Try it »
Definition and usage
The querySelectorAll() method returns all elements in the document that match the specified CSS selector, returning a NodeList object.
NodeList object representing a collection of nodes. It can be accessed by index, with index values starting from 0.
Tip: You can use the NodeList object's length property to get the element attributes that match the selector, then you can traverse all elements to get the information you want.
For more CSS selectors, you can refer to CSS Selector Tutorial , CSS Selector Reference Manual.
Browser support
The numbers in the table indicate the first browser version that supports this method.
| Method | |||||
|---|---|---|---|---|---|
| querySelectorAll() | 4.0 | 8.0 | 3.5 | 3.2 | 10.0 |
Note: Internet Explorer 8 supports CSS2 selectors. Browsers of IE9 and higher already support CSS3 selectors.
Syntax
elementList = document.querySelectorAll(selectors);
- elementList is a static object of type NodeList.
- selectors is a string containing one or more CSS selectors connected by commas.
Attribute values
| Parameters | Type | Description |
|---|---|---|
| CSS selector | String | Required. Specifies one or more elements that match the CSS selector. Elements can be obtained by using id, class, type, attribute, attribute value, etc. as selectors. |
多个选择器使用逗号(,)分隔。
**Hint:** For more information on CSS selectors, please refer to [CSS Selector Reference Manual](/cssref/css-selectors.html). |
Method
| DOM version: | Level 1 Document Object |
|---|---|
| Return value: | A NodeList object representing all elements in the document that match the specified CSS selector. NodeList is a static object of type NodeList. If the specified selector is invalid, a SYNTAX_ERR exception is thrown. |
More examples
Example
Get all the
elements in the document, and set the background color for the first matching
element (index 0):
//
Get all the
Element
var
x
=
document
.
querySelectorAll
(
"
p
"
)
;
//
Set the first
element's background color
x
[
0
]
.
style
.
backgroundColor
=
"
red
"
;
Try it »
Example
Get all elements with class="example" in the document
elements in the document, and set the background color for the first matching
element (index 0):
//
Get all elements with class="example" in the document
Element
var
x
=
document
.
querySelectorAll
(
"
p.example
"
)
;
//
Set the first element with class="example"
element's background color
x
[
0
]
.
style
.
backgroundColor
=
"
red
"
;
Try it »
Example
Count the elements with class="example" in the document
number of elements (using the length property of the NodeList object):
var
x
=
document
.
querySelectorAll
(
"
.example
"
)
.
length
;
Try it »
Example
Set the background color of all elements with class="example" in the document:
var
x
=
document
.
querySelectorAll
(
"
.example
"
)
;
var
i
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
x
[
i
]
.
style
.
backgroundColor
=
"
red
"
;
}
Try it »
Example
Set all the
elements' background color:
var
x
=
document
.
querySelectorAll
(
"
p
"
)
;
var
i
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
x
[
i
]
.
style
.
backgroundColor
=
"
red
"
;
}
Try it »
Example
Find elements in the document that contain the "target" attribute tags, and set a border for them:
var
x
=
document
.
querySelectorAll
(
"
a[target]
"
)
;
var
i
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
x
[
i
]
.
style
.
border
=
"
10px solid red
"
;
}
Try it »
Example
Find each element whose parent element is a
element, and set its background color:
var
x
=
document
.
querySelectorAll
(
"
div > p
"
)
;
var
i
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
x
[
i
]
.
style
.
backgroundColor
=
"
red
"
;
}
Try it »
Example
Set the background color for all the
,
and elements in the document:
var
x
=
document
.
querySelectorAll
(
"
h2, div, span
"
)
;
var
i
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
x
[
i
]
.
style
.
backgroundColor
=
"
red
"
;
}
Try it »
Related Articles
CSS Reference Manual: CSS Selector Reference Manual
JavaScript tutorial: JavaScript HTML DOM Node List
HTML DOM Reference Manual: document.querySelector()
HTML DOM Reference Manual: element.querySelectorAll()
var
x
=
document
.
querySelectorAll
(
"
h2, div, span
"
)
;
var
i
;
for
(
i
=
0
;
i
<
x
.
length
;
i
++
)
{
x
[
i
]
.
style
.
backgroundColor
=
"
red
"
;
}
Try it »
Related Articles
CSS Reference Manual: CSS Selector Reference Manual
JavaScript tutorial: JavaScript HTML DOM Node List
HTML DOM Reference Manual: document.querySelector()
HTML DOM Reference Manual: element.querySelectorAll()
Document Object