Baseline Widely available
The matches()
method of the Element
interface tests whether the element would be selected by the specified CSS selector.
selectors
A string containing valid CSS selectors to test the Element
against.
true
if the Element
matches the selectors
. Otherwise, false
.
SyntaxError
DOMException
Thrown if 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>
JavaScript
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
.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4