Element.matches

  • Revision slug: DOM/Element.mozMatchesSelector
  • Revision title: Element.mozMatchesSelector
  • Revision id: 353909
  • Created:
  • Creator: mattbasta
  • Is current revision? No
  • Comment

Revision Content

{{ DomRef() }}

{{ gecko_minversion_header("1.9.2") }}

Summary

Returns true if the element would be selected by the specified selector string; otherwise, returns false.

Syntax

result = element.mozMatchesSelector(selectorString) 
  • result holds the return value true or false.
  • selectorString is a string representing the selector to test.

Example

  <div id="foo">This is the element!</div>
  <script type="text/javascript">
    var el = document.getElementById("foo");
    if (el.mozMatchesSelector("div")) {
      alert("Match!");
    }
  </script>

This will alert, since the element is indeed a {{ HTMLElement("div") }}.

Exceptions

SYNTAX_ERR {{ gecko_minversion_inline("2.0") }}
The specified selector string is invalid. Prior to Gecko 2.0, invalid selector strings caused false to be returned instead of throwing an exception.

Specification

{{ languages( {"zh-cn": "zh-cn/dom/element.mozmatchesselector" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<p>{{ gecko_minversion_header("1.9.2") }}</p>
<h3 id="Summary" name="Summary">Summary</h3>
<p>Returns <code>true</code> if the element would be selected by the specified selector string; otherwise, returns <code>false</code>.</p>
<h3 id="Syntax" name="Syntax">Syntax</h3>
<pre class="eval">
<em>result</em> = <em>element</em>.mozMatchesSelector(selectorString) 
</pre>
<ul>
  <li><code>result</code> holds the return value <code>true</code> or <code>false</code>.</li>
  <li><code>selectorString</code> is a string representing the selector to test.</li>
</ul>
<h3 id="Example" name="Example">Example</h3>
<pre>
  &lt;div id="foo"&gt;This is the element!&lt;/div&gt;
  &lt;script type="text/javascript"&gt;
    var el = document.getElementById("foo");
    if (el.mozMatchesSelector("div")) {
      alert("Match!");
    }
  &lt;/script&gt;
</pre>
<p>This will alert, since the element is indeed a {{ HTMLElement("div") }}.</p>
<h3 id="Exceptions">Exceptions</h3>
<dl>
  <dt>
    <code>SYNTAX_ERR</code> {{ gecko_minversion_inline("2.0") }}</dt>
  <dd>
    The specified selector string is invalid. Prior to Gecko 2.0, invalid selector strings caused <code>false</code> to be returned instead of throwing an exception.</dd>
</dl>
<h3 id="Specification">Specification</h3>
<ul>
  <li><a class="vt-p external" href="http://dev.w3.org/2006/webapi/selectors-api2/#matchtesting" title="http://dev.w3.org/2006/webapi/selectors-api2/#matchtesting">Selectors API&nbsp;2</a> - Draft specification</li>
</ul>
<p>{{ languages( {"zh-cn": "zh-cn/dom/element.mozmatchesselector" } ) }}</p>
Revert to this revision