HTML DOM Element matches() method
- Previous Page lastElementChild
- Next Page namespaceURI
- Go Up One Level HTML DOM Elements Object
Definition and usage
matches()
The method will return true
, if the element matches a specific CSS selector; otherwise return false
.
Also see:
Example
Example 1
Does the element match a CSS selector?
const element = document.getElementById("demo"); let answer = element.matches(".container");
Example 2
Does the element match one of the two selectors?
const element = document.getElementById("demo"); let answer = element.matches(".container, .wrapper");
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 |
|
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 |
- Previous Page lastElementChild
- Next Page namespaceURI
- Go Up One Level HTML DOM Elements Object