JavaScript matches() Method Tutorial

In this section, we will learn what the Element matches() method is and how to use it in JavaScript.

What is Element matches() Method in JavaScript?

The JavaScript Element matches() method is used to check the element that invoked this method to see if it matches a specific CSS Selector or not.

For example, if we want to know if the target element has the class attribute with the value “clr” then we can run the method like this:

element.matches(“.crl”);

JavaScript matches() Method Syntax:

element.matches(“CSS-Selector”);

JavaScript matches() Method Parameter

The method takes one argument and that is the CSS selector we want to compare the element with.

JavaScript matches() Method Return Value

The return value of this method is of type boolean and will be equal to true if the element matched the CSS Selector. Other the value false will return instead.

Example: matches() in JavaScript

See the Pen matches() in JavaScript by Omid Dehghan (@odchan1) on CodePen.

Leave a Reply