Element.querySelector

  • Revision slug: DOM/Element.querySelector
  • Revision title: Element.querySelector
  • Revision id: 364893
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment +spec

Revision Content

{{ DomRef() }}

Summary

Returns the first element that is a descendent of the element on which it is invoked that matches the specified group of selectors.

Syntax

element = baseElement.querySelector(selectors);

where

  • element and baseElement are element objects.
  • selectors is a group of selectors to match on.

Example

In this example, the first style element which either has no type or has type text/css in the HTML document body is returned:

var el = document.body.querySelector("style[type='text/css'], style:not([type])");

Notes

Returns null if no matches are found; otherwise, it returns the first matching element.

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

querySelector() was introduced in the WebApps API.

Browser compatibility

Browser Support Notes
Internet Explorer 8  
Firefox (Gecko) 3.5 (1.9.1)  
Opera 10  
Chrome 1  
Safari (webkit) 3.2 (525.3) webk.it/16587

Specification

See also

Revision Source

<p>{{ DomRef() }}</p>
<p>Summary</p>
<p>Returns the first element that is a descendent of the element on which it is invoked that matches the specified group of selectors.</p>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="eval"><em>element</em> = baseElement.querySelector(<em>selector</em>s);
</pre>
<p>where</p>
<ul>
  <li><code>element</code> and <code>baseElement</code> are <a href="/en/DOM/element" title="en/DOM/element">element</a> objects.</li>
  <li><code>selectors</code> is a group of selectors to match on.</li>
</ul>
<h3 id="Example" name="Example">Example</h3>
<p>In this example, the first <code>style</code> element which either has no type or has type <code>text/css</code> in the HTML document body is returned:</p>
<pre class="brush: js">var el = document.body.querySelector("style[type='text/css'], style:not([type])");
</pre>
<h3 id="Notes" name="Notes">Notes</h3>
<p>Returns <code>null</code> if no matches are found; otherwise, it returns the first matching element.</p>
<p>Throws a <code>SYNTAX_ERR</code> exception if the specified group of selectors is invalid.</p>
<p><span style="font-family: Courier New;"><span>querySelector()</span></span> was introduced in the WebApps API.</p>
<h3 id="Browser_compatibility">Browser compatibility</h3>
<table class="standard-table">
  <tbody>
    <tr>
      <th>Browser</th>
      <th>Support</th>
      <th>Notes</th>
    </tr>
    <tr>
      <td>Internet Explorer</td>
      <td>8</td>
      <td> </td>
    </tr>
    <tr>
      <td>Firefox (Gecko)</td>
      <td><strong>3.5</strong> (1.9.1)</td>
      <td> </td>
    </tr>
    <tr>
      <td>Opera</td>
      <td>10</td>
      <td> </td>
    </tr>
    <tr>
      <td>Chrome</td>
      <td>1</td>
      <td> </td>
    </tr>
    <tr>
      <td>Safari (webkit)</td>
      <td>3.2 (525.3)</td>
      <td><a class="link-https" href="https://bugs.webkit.org/show_bug.cgi?id=16587">webk.it/16587</a></td>
    </tr>
  </tbody>
</table>
<h3 id="Specification" name="Specification">Specification</h3>
<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>
</ul>
<h3 id="See_also" name="See_also">See also</h3>
<ul>
  <li><a class="internal" href="/En/DOM/Element.querySelectorAll" title="En/DOM/element.querySelectorAll"><code>element.querySelectorAll</code></a></li>
  <li><a class="internal" href="/En/DOM/Document.querySelector" title="en/DOM/document.querySelector"><code>document.querySelector</code></a></li>
  <li><a class="internal" href="/En/DOM/Document.querySelectorAll" title="en/DOM/document.querySelectorAll"><code>document.querySelectorAll</code></a></li>
  <li><code><a href="/En/Code_snippets/QuerySelector" title="En/Code_snippets/QuerySelector">Code snippets for querySelector</a></code></li>
</ul>
Revert to this revision