document.querySelectorAll

  • Revision slug: Web/API/Document.querySelectorAll
  • Revision title: Document.querySelectorAll
  • Revision id: 400173
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Moved From DOM/Document.querySelectorAll to Web/API/Document.querySelectorAll

Revision Content

Summary

Returns a list of the elements within the document (using depth-first pre-order traversal of the document's nodes) that match the specified group of selectors. The object returned is a NodeList.

Syntax

elementList = document.querySelectorAll(selectors);

where

  • elementList is a non-live NodeList of element objects.
  • selectors is a string containing one or more CSS selectors separated by commas.

The returned NodeList will contain all the elements in the document that are matched by any of the specified selectors. If the selectors string contains a CSS pseudo-element, the returned elementList will be empty.

Example

This example returns a list of all div elements within the document with a class of either "note" or "alert":

var matches = document.querySelectorAll("div.note, div.alert");

Notes

Returns a non-live NodeList of all the matching element nodes.

Throws a SYNTAX_ERR exception if the specified group of selectors is invalid.

querySelectorAll() was introduced in the WebApps API.

WebKit browsers have a bug: when the selectors string contains a CSS pseudo-element, the returned elementList is not empty but contains the {{HTMLElement("html")}} element.

Browser compatibility

{{CompatibilityTable}}
Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 1 3.5 (1.9.1) 9
8 (CSS2 selectors only)
10 3.2 (525.3)
Feature Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support 2.1 yes 9 10.0 3.2

Specification

See also

Revision Source

<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a list of the elements within the document (using depth-first pre-order traversal of the document's nodes) that match the specified group of selectors. The object returned is a <a href="/en-US/docs/DOM/NodeList" title="DOM/NodeList">NodeList</a>.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<var>elementList</var> = document.querySelectorAll(<em>selectors</em>);
</pre>
<p>where</p>
<ul>
  <li><code>elementList</code> is a non-live <a href="/en-US/docs/DOM/NodeList" title="DOM/NodeList">NodeList</a> of <a href="/en-US/docs/DOM/element" title="DOM/element">element</a> objects.</li>
  <li><code><em>selectors</em></code> is a string containing one or more CSS selectors separated by commas.</li>
</ul>
<p>The returned NodeList will contain all the elements in the document that are matched by any of the specified selectors. If the <code><em>selectors</em></code> string contains a <a href="/en-US/docs/CSS/Pseudo-elements" title="Pseudo-elements">CSS pseudo-element</a>, the returned <code><var>elementList</var></code> will be empty.</p>
<h2 id="Example" name="Example">Example</h2>
<p>This example returns a list of all <code>div</code> elements within the <code>document</code> with a class of either "<code>note</code>" or "<code>alert</code>":</p>
<pre class="brush: js">
var matches = document.querySelectorAll("div.note, div.alert");
</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<p>Returns a non-live <code>NodeList</code> of all the matching element nodes.</p>
<p>Throws a <code>SYNTAX_ERR</code> exception if the specified group of selectors is invalid.</p>
<p><code>querySelectorAll()</code> was introduced in the WebApps API.</p>
<p>WebKit browsers have a bug: when the <code><em>selectors</em></code> string contains a <a href="/en-US/docs/CSS/Pseudo-elements" rel="internal" title="Pseudo-elements">CSS pseudo-element</a>, the returned <code><var>elementList</var></code> is not empty but contains the {{HTMLElement("html")}} element.</p>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<div>{{CompatibilityTable}}</div>
<div id="compat-desktop">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Chrome</th>
        <th>Firefox (Gecko)</th>
        <th>Internet Explorer</th>
        <th>Opera</th>
        <th>Safari (WebKit)</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>1</td>
        <td>3.5 (1.9.1)</td>
        <td>9<br />
          8 (CSS2 selectors only)</td>
        <td>10</td>
        <td>3.2 (525.3)</td>
      </tr>
    </tbody>
  </table>
</div>
<div id="compat-mobile">
  <table class="compat-table">
    <tbody>
      <tr>
        <th>Feature</th>
        <th>Android</th>
        <th>Firefox Mobile (Gecko)</th>
        <th>IE Mobile</th>
        <th>Opera Mobile</th>
        <th>Safari Mobile</th>
      </tr>
      <tr>
        <td>Basic support</td>
        <td>2.1</td>
        <td>yes</td>
        <td>9</td>
        <td>10.0</td>
        <td>3.2</td>
      </tr>
    </tbody>
  </table>
</div>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a href="http://www.w3.org/TR/selectors-api/" title="http://www.w3.org/TR/selectors-api/">Selectors API</a></li>
</ul>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
  <li><a href="/en-US/docs/DOM/Element.querySelectorAll" title="DOM/element.querySelectorAll"><code>element.querySelectorAll</code></a></li>
  <li><a href="/en-US/docs/DOM/Document.querySelector" title="DOM/document.querySelector"><code>document.querySelector</code></a></li>
  <li><a href="/en-US/docs/DOM/Element.querySelector" title="DOM/element.querySelector"><code>element.querySelector</code></a></li>
  <li><a href="/en-US/docs/Code_snippets/QuerySelector" title="Code_snippets/QuerySelector">Code snippets for <code>querySelector</code></a></li>
</ul>
Revert to this revision