How to check if a DOM element has a class
New Courses Coming Soon
Join the waiting lists
How do you check if a particular DOM element you have the reference of, has a class?
Use the contains
method provided by the classList
object, which is:
element.classList.contains('myclass')
Technically, classList is an object that satisfies the DOMTokenList
interface, which means it implements its methods and properties.
You can see its details on the DOMTokenList MDN page.
→ Read my DOM Tutorial on
The Valley of Code
→ Read my
Browser Events Tutorial
on The Valley of Code
→ Read my
Browser APIs Tutorials
on The Valley of Code
Here is how can I help you:
- COURSES where I teach everything I know
- CODING BOOTCAMP cohort course - next edition in 2025
- BOOKS 16 coding ebooks you can download for free on JS Python C PHP and lots more
- Follow me on X