Locating DOM elements using selectors

  • Revision slug: DOM/Locating_DOM_elements_using_selectors
  • Revision title: Locating DOM elements using selectors
  • Revision id: 298599
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{ gecko_minversion_header("1.9.1") }}

The Selectors API provides methods that make it quick and easy to retrieve Element nodes from the DOM by matching against a set of selectors. This is much faster than past techniques, wherein it was necessary to, for example, use a loop in JavaScript code to locate the specific items you needed to find.

The NodeSelector interface

This specification adds two new methods to any objects implementing the Document, DocumentFragment, or Element interfaces:

querySelector
Returns the first matching Element node within the node's subtree. If no matching node is found, null is returned.
querySelectorAll
Returns a NodeList containing all matching Element nodes within the node's subtree, or an empty NodeList if no matches are found.
Note: The NodeList returned by querySelectorAll() is not live. This is different from other DOM querying methods that return live node lists.

You may find examples and details by reading the documentation for the querySelector() and querySelectorAll() methods, as well as in the article Code snippets for querySelector.

Selectors

The selector methods accept one or more comma-separated selectors to determine what element or elements should be returned. For example, to select all paragraph (p) elements in a document whose CSS class is either warning or note, you can do the following:

var special = document.querySelectorAll( "p.warning, p.note" );

You can also query by ID. For example:

var el = document.querySelector( "#main, #basic, #exclamation" );

After executing the above code, el contains the first element in the document whose ID is one of main, basic, or exclamation.

You may use any CSS selectors with the querySelector() and querySelectorAll() methods.

See also

Revision Source

<div>{{ gecko_minversion_header("1.9.1") }}</div>

<p>The Selectors API provides methods that make it quick and easy to retrieve <a class="internal" href="/en-US/docs/DOM/element" title="en-US/docs/DOM/Element"><code>Element</code></a> nodes from the DOM by matching against a set of selectors. This is much faster than past techniques, wherein it was necessary to, for example, use a loop in JavaScript code to locate the specific items you needed to find.</p>
<h2 id="The_NodeSelector_interface">The NodeSelector interface</h2>
<p>This specification adds two new methods to any objects implementing the <a class="internal" href="/en-US/docs/DOM/document" title="en-US/docs/DOM/Document"><code>Document</code></a>, <a class="internal" href="/en-US/docs/DOM/DocumentFragment" title="en-US/docs/DOM/DocumentFragment"><code>DocumentFragment</code></a>, or <a class="internal" href="/en-US/docs/DOM/element" title="en-US/docs/DOM/Element"><code>Element</code></a> interfaces:</p>
<dl>
  <dt><code>querySelector</code></dt>
  <dd>Returns the first matching <a class="internal" href="/en-US/docs/DOM/element" title="en-US/docs/DOM/Element"><code>Element</code></a> node within the node's subtree. If no matching node is found, <code>null</code> is returned.</dd>
  <dt><code>querySelectorAll</code></dt>
  <dd>Returns a <a class="internal" href="/en-US/docs/DOM/NodeList" title="en-US/docs/DOM/NodeList"><code>NodeList</code></a> containing all matching <code>Element</code> nodes within the node's subtree, or an empty <code>NodeList</code> if no matches are found.</dd>
</dl>

<div class="note">
  <strong>Note:</strong> The <code><a class="internal" href="/en-US/docs/DOM/NodeList" title="en-US/docs/DOM/NodeList">NodeList</a></code> returned by <code><a class="internal" href="/en-US/docs/DOM/Element.querySelectorAll" title="en-US/docs/DOM/Element.querySelectorAll">querySelectorAll()</a></code> is not live. This is different from other DOM querying methods that return live node lists.</div>
<p>You may find examples and details by reading the documentation for the <a class="internal" href="/en-US/docs/DOM/Element.querySelector" title="en-US/docs/DOM/Element.querySelector"><code>querySelector()</code></a> and <a class="internal" href="/en-US/docs/DOM/Element.querySelectorAll" title="en-US/docs/DOM/Element.querySelectorAll"><code>querySelectorAll()</code></a> methods, as well as in the article <a class="internal" href="/en-US/docs/Code_snippets/QuerySelector" title="en-US/docs/Code snippets/QuerySelector">Code snippets for querySelector</a>.</p>

<h2 id="Selectors">Selectors</h2>
<p>The selector methods accept one or more comma-separated selectors to determine what element or elements should be returned. For example, to select all paragraph (<code>p</code>) elements in a document whose CSS class is either <code>warning</code> or <code>note</code>, you can do the following:</p>

<pre><code>var special = document.querySelectorAll( "p.warning, p.note" );</code></pre>
<p>You can also query by ID. For example:</p>
<pre><code>var el = document.querySelector( "#main, #basic, #exclamation" );</code></pre>

<p>After executing the above code, <code>el</code> contains the first element in the document whose ID is one of <code>main</code>, <code>basic</code>, or <code>exclamation</code>.</p>

<p>You may use any CSS selectors with the <code>querySelector()</code> and <code>querySelectorAll()</code> methods.</p>

<h2 id="See_also">See also</h2>
<ul>
  <li><a class="external" href="http://www.w3.org/TR/selectors-api/" title="http://www.w3.org/TR/selectors-api/">Selectors API</a></li>
  <li><a href="/en-US/docs/DOM/Element.querySelector" title="en-US/docs/DOM/Element.querySelector"><code>element.querySelector</code></a></li>
  <li><a href="/en-US/docs/DOM/Element.querySelectorAll" title="en-US/docs/DOM/element.querySelectorAll"><code>element.querySelectorAll</code></a></li>
  <li><a href="/en-US/docs/DOM/Document.querySelector" title="en-US/docs/DOM/document.querySelector"><code>document.querySelector</code></a></li>
  <li><a href="/en-US/docs/DOM/Document.querySelectorAll" title="en-US/docs/DOM/document.querySelectorAll"><code>document.querySelectorAll</code></a></li>
  <li><a href="/en-US/docs/Code_snippets/QuerySelector" title="en-US/docs/Code_snippets/QuerySelector">Code snippets for querySelector</a></li>
</ul>
Revert to this revision