files/en-us/web/api/element/matches/index.md
{{APIRef("DOM")}}
The matches() method of the {{domxref("Element")}} interface tests whether the element would be selected by the specified CSS selector.
matches(selectors)
selectors
true if the {{domxref("Element")}} matches the selectors. Otherwise, false.
SyntaxError {{domxref("DOMException")}}
selectors cannot be parsed as a CSS selector list.<ul id="birds">
<li>Orange-winged parrot</li>
<li class="endangered">Philippine eagle</li>
<li>Great white pelican</li>
</ul>
const birds = document.querySelectorAll("li");
for (const bird of birds) {
if (bird.matches(".endangered")) {
console.log(`The ${bird.textContent} is endangered!`);
}
}
This will log "The Philippine eagle is endangered!" to the console, since the element
has indeed a class attribute with value endangered.
{{Specifications}}
{{Compat}}