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.

Also see:

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 refer to 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
February 2014 April 2017 December 2014 October 2013 May 2014