Skip to main content Accessibility Feedback

Selectors

Element.matches()

Check if an element would be selected by a particular selector or set of selectors. Returns true if the element is a match, and false when it’s not.

// Check if the first .bg-red element has the [data-snack attribute]
let red = document.querySelector('.bg-red');
if (red.matches('[data-snack]')) {
	console.log('Yummy snack!');
} else {
	console.log('No snacks');
}

Preorder my new course on Web Components! Want to learn how to build Web Components from scratch, master best practices, and more? Preorder today and get $100 off of the launch price.


Find this useful? You can support my work by purchasing an annual membership.