151 of 264 menu

matches method

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

Syntax

element.matches('selector');

Example

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

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

The code execution result:

true

See also

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