HTML DOM Element matches() method

Definition and usage

matches() The method will return true, if the element matches a specific CSS selector; otherwise return false.

See also:

Complete CSS selector reference manual

Element closest() method

Example

Example 1

Does the element match a CSS selector?

const element = document.getElementById("demo");
let answer = element.matches(".container");

Try it yourself

Example 2

Does the element match one of the two selectors?

const element = document.getElementById("demo");
let answer = element.matches(".container, .wrapper");

Try it yourself

Syntax

element.matches(selectors)

Parameter

Parameter Description
selectors

Required. One or more (comma-separated) CSS selectors to match.

The returned element is the first element found in the document.

Please see our complete CSS selector reference manual.

Return value

Type Description
Boolean value
  • true - Element matches the CSS selector
  • false - Element does not match the CSS selector

Browser support

First fully supported matches() Browser version of the method:

Chrome Edge Firefox Safari Opera
Chrome
33
Edge
15
Firefox
34
Safari
7
Opera
21
2014 February 2017 April 2014 December October 2013 May 2014