Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

JavaScript Dictionary

  1. Home
  2. JavaScript Dictionary
  3. document.querySelectorAll()

document.querySelectorAll() Since: Selectors API(2008)

Uses a CSS selector to retrieve all HTML elements in the document that match the given condition. The return value is a NodeList object. See the overview for details on the NodeList object.

Syntax

// Retrieves from the entire document.
var elements = document.querySelectorAll("selector");

// Retrieves from within a specific HTML element.
var elements = parentElement.querySelectorAll("selector");

Arguments

ArgumentDescription
selectorSpecifies a CSS selector string. Any selector format that can be used in CSS is accepted. You can also specify multiple selectors separated by commas.

Return Value

Returns a NodeList object containing all HTML elements that match the condition. The NodeList object can be treated like an array. If no matching HTML elements exist, an empty NodeList is returned.

Sample Code

<ul>
	<li class="item">Apple</li>
	<li class="item">Orange</li>
	<li class="item">Grape</li>
</ul>
// Retrieves all li elements.
var items = document.querySelectorAll(".item");
console.log(items.length); // Outputs "3".

// Loops through with forEach.
items.forEach(function(item) {
	console.log(item.textContent);
});
// "Apple" "Orange" "Grape"

Overview

document.querySelectorAll() is a method that retrieves all HTML elements matching a condition as a NodeList. While document.querySelector() returns only the first match, this method returns all matching HTML elements.

The returned NodeList is an array-like object that supports the length property and index-based access. You can also iterate over it using the forEach() method.

Note that the NodeList is a snapshot taken at the time of the call and will not automatically reflect HTML elements added to the DOM afterward. To get the latest state, you need to call document.querySelectorAll() again.

document.querySelectorAll() is covered in more detail in the tutorial article querySelector() — Selecting Elements with CSS Selectors. Feel free to check it out.

Browser Compatibility

Chrome Chrome
49+
Supported in all versions
Firefox Firefox
57+
2.5 or earlier ×
Safari Safari
18+
3 or earlier ×
Edge Edge
80+
11 or earlier ×
IE IE
11+
8 or earlier ×
Opera Opera
48+
9 or earlier ×
iOS Safari iOS Safari
18+
1 or earlier ×
Android Browser Android Browser
37+
4 or earlier ×
Chrome Android Chrome Android
36+
17 or earlier ×
Firefox Android Firefox Android
79+
3 or earlier ×

If you find any errors or copyright issues, please .