Element: matches() method

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since April 2017.

The matches() method of the Element interface tests whether the element would be selected by the specified CSS selector.

Syntax

js
matches(selectors)

Parameters

selectors

A string containing valid CSS selectors to test the Element against.

Return value

true if the Element matches the selectors. Otherwise, false.

Exceptions

SyntaxError DOMException

Thrown if selectors cannot be parsed as a CSS selector list.

Examples

HTML

html
<ul id="birds">
  <li>Orange-winged parrot</li>
  <li class="endangered">Philippine eagle</li>
  <li>Great white pelican</li>
</ul>

JavaScript

js
const birds = document.querySelectorAll("li");

for (const bird of birds) {
  if (bird.matches(".endangered")) {
    console.log(`The ${bird.textContent} is endangered!`);
  }
}

This will log "The Philippine eagle is endangered!" to the console, since the element has indeed a class attribute with value endangered.

Specifications

Specification
DOM
# ref-for-dom-element-matches①

Browser compatibility

desktop mobile
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
matches
Chrome – Full support
Chrome 4 (Release date: 2010-01-25)
altname
altname Alternate name: webkitMatchesSelector
Chrome – Full support
Chrome 33 (Release date: 2014-02-20)
footnote Full support
Edge – No support
Edge 12 – 18 (Release date: 2015-07-29)
altname
footnote Removed in 79 and later
altname Alternate name: msMatchesSelector
Edge – Full support
Edge 12 (Release date: 2015-07-29)
altname
altname Alternate name: webkitMatchesSelector
Edge – Full support
Edge 15 (Release date: 2017-04-05)
footnote Full support
Firefox – Full support
Firefox 3.6 (Release date: 2010-01-21)
footnote , altname
altname Alternate name: mozMatchesSelector
footnote Before Firefox 4, invalid selector strings caused false to be returned instead of throwing an exception.
footnote See bug 1119718 for removal.
Firefox – Full support
Firefox 44 (Release date: 2016-01-26)
altname
altname Alternate name: webkitMatchesSelector
Firefox – Full support
Firefox 34 (Release date: 2014-12-01)
footnote Full support
Opera – No support
Opera 11.5 – 12.1 (Release date: 2011-06-28)
altname
altname Alternate name: oMatchesSelector
Opera – Full support
Opera 15 (Release date: 2013-07-02)
altname
altname Alternate name: webkitMatchesSelector
Opera – Full support
Opera 21 (Release date: 2014-05-06)
footnote Full support
Safari – Full support
Safari 5 (Release date: 2010-06-07)
altname
altname Alternate name: webkitMatchesSelector
Safari – Full support
Safari 8 (Release date: 2014-10-16)
footnote Full support
Chrome Android – Full support
Chrome Android 18 (Release date: 2012-06-27)
altname
altname Alternate name: webkitMatchesSelector
Chrome Android – Full support
Chrome Android 33 (Release date: 2014-02-26)
footnote Full support
Firefox for Android – Full support
Firefox for Android 4 (Release date: 2011-03-29)
footnote , altname
altname Alternate name: mozMatchesSelector
footnote See bug 1119718 for removal.
Firefox for Android – Full support
Firefox for Android 44 (Release date: 2016-01-26)
altname
altname Alternate name: webkitMatchesSelector
Firefox for Android – Full support
Firefox for Android 34 (Release date: 2014-12-01)
footnote Full support
Opera Android – No support
Opera Android 11.5 – 12.1 (Release date: 2011-10-12)
altname
altname Alternate name: oMatchesSelector
Opera Android – Full support
Opera Android 14 (Release date: 2013-05-21)
altname
altname Alternate name: webkitMatchesSelector
Opera Android – Full support
Opera Android 21 (Release date: 2014-04-22)
footnote Full support
Safari on iOS – Full support
Safari on iOS 4.2 (Release date: 2010-11-22)
altname
altname Alternate name: webkitMatchesSelector
Safari on iOS – Full support
Safari on iOS 8 (Release date: 2014-09-17)
footnote Full support
Samsung Internet – Full support
Samsung Internet 1 (Release date: 2013-04-27)
altname
altname Alternate name: webkitMatchesSelector
Samsung Internet – Full support
Samsung Internet 2 (Release date: 2014-10-17)
footnote Full support
WebView Android – Full support
WebView Android 4.4 (Release date: 2013-12-09)
altname
altname Alternate name: webkitMatchesSelector
WebView Android – Full support
WebView Android 4.4.3 (Release date: 2014-06-02)
footnote Full support
WebView on iOS – Full support
WebView on iOS 4.2 (Release date: 2010-11-22)
altname
altname Alternate name: webkitMatchesSelector
WebView on iOS – Full support
WebView on iOS 8 (Release date: 2014-09-17)
footnote Full support

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support
Uses a non-standard name
Has more compatibility info.

See also