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');
}

Join the Lean Web Club! Coaching. Courses. Coding resources. Get the skills, confidence, and support you need to learn front-end web development and achieve long-term success.


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