HTML DOM Element matches()
Examples
Does the element match a CSS selector?
const element = document.getElementById("demo");
let answer = element.matches(".container");
Try it Yourself »
Does the element match one of two selectors?:
const element = document.getElementById("demo");
let answer = element.matches(".container, .wrapper");
Try it Yourself »
Description
The matches()
method returns true
if an element matches a specific CSS selector(s).
matches()
method returns false
if not.
Syntax
element.matches(selectors)
Parameters
Parameter | Description |
selectors |
Required. One or more (comma separeted) CSS selectors to match. The returned element is the first found in the document. See our Complete CSS Selectors Reference. |
Return Value
Type | Description |
Boolean | true - The element matches the CSS selector.false - The element does not match the CSS selector. |
Browser Support
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 |
Copyright 1999-2023 by Refsnes Data. All Rights Reserved.