Check if an element includes a certain class with "matches"
- Published at
- Updated at
- Reading time
A common operation when building interfaces is to figure out if an element contains a particular class. Today I learned about
Element. The element method can be used to check if an element includes a certain class and is way shorter than
const elem = document.querySelector('.foo'); elem.classList.contains('bar'); // true elem.matches('.bar'); // true