Element.matches()
The DOM method that returns true if an element would be selected by a given CSS selector.
In more detail
It accepts any CSS selector string. It tests one element without searching the tree, which makes it a quick check inside event handlers, such as whether the clicked element is a delete button.
Related terms
- Element.closest() — The DOM method that starts at an element and walks up through its ancestors, returning the first one, itself included, that matches a selector.
- CSS selector — A pattern, such as .card, #menu or nav a, that picks which elements a CSS rule applies to or which elements a DOM query returns.
- event delegation — Handling events for many child elements with one listener on a shared ancestor, using event.target to find which child was involved.
Taught in
- Modern DOM Querying: querySelector, closest And matches — Frontend Architecture & Web Platform
- Event Delegation: Memory-Efficient Interaction Handlers — Frontend Architecture & Web Platform