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

sample_querySelectorAll.html
<ul id="sorcerers">
	<li class="member grade-special">Gojo Satoru</li>
	<li class="member grade-1">Ryomen Sukuna</li>
	<li class="member grade-1">Itadori Yuji</li>
	<li class="member grade-1">Fushiguro Megumi</li>
	<li class="member grade-1">Kugisaki Nobara</li>
</ul>
sample_querySelectorAll.js
// Pattern 1: Get all elements by class name
var members = document.querySelectorAll(".member");
console.log(members.length); // Outputs "5"

// Pattern 2: Loop through with forEach
members.forEach(function(el) {
	console.log(el.textContent);
});
// Outputs "Gojo Satoru", "Ryomen Sukuna", "Itadori Yuji", "Fushiguro Megumi", "Kugisaki Nobara" in order.

// Pattern 3: Filter by a more specific class
var grade1 = document.querySelectorAll(".grade-1");
console.log(grade1.length); // Outputs "4"

// Pattern 4: Access a specific element by index
var first = document.querySelectorAll(".member")[0];
console.log(first.textContent); // Outputs "Gojo Satoru"

// Pattern 5: When no element matches, an empty NodeList is returned (not null)
var none = document.querySelectorAll(".nonexistent");
console.log(none.length); // Outputs "0"
if (none.length > 0) {
	// Handle found elements
}

View sample page

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+
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 .