matches method

The matches method allows you to check if an element matches the specified CSS selector.

Syntax

javascriptelement.matches('selector');

Example

We check if our element is the paragraph with the class www:

html<p id="elem" class="www"></p>
javascriptlet elem = document.querySelector('#elem');
console.log(elem.matches('p.www'));

The code execution result:

javascripttrue

See also

  • the contains method
    that checks a child by a selector
  • the closest method
    that finds a parent by a selector