HTML DOM matches()Method
Example
Does the element match the CSS selector?
const element = document.getElementById("demo");
let answer = element.matches(".container");
Try it Yourself »
Example
Does the element match one of two CSS selectors?
const element = document.getElementById("demo");
let answer = element.matches(".container, .wrapper");
Try it Yourself »
Definition and Usage
If the element matches the specified CSS selector, the matches() method returns true, otherwise it returns false.
Browser Support
The numbers in the table specify the first browser versions that fully support the matches() method:
| Chrome 33 |
Edge 15 |
Firefox 34 |
Safari 7 |
Opera 21 |
| Feb 2014 | Apr 2017 | Dec 2014 | Oct 2013 | May 2014 |
Syntax
element.matches(selectors)
Parameters
| Parameter | Description |
| selectors | Required. One or more CSS selectors to match (separate multiple with commas ,). The returned element is the first element found in the HTML document. View AllCSS Selector Reference。 |
Return Value
| Type | Description |
|---|---|
| Boolean | true - The element matches the CSS selector. false - The element does not match the CSS selector. |
Other Extensions
Element Object