How to check if an element has a class in JavaScript
In this post we will give you information about How to check if an element has a class in JavaScript. Hear we will give you detail about How to check if an element has a class in JavaScriptAnd how to use it also give you demo for it if it is necessary.
we will learn how to check if an HTML element has a particular class or not using JavaScript.
classList.contains() method
We can use the classList.contains() method to check if an element has a class name or not.
The classList.contains() method returns true if a class name is found else it returns false (if a class name is not found).
Example:
<div id="first"> <h1>Hello happy people</h1></div>
const div = document.getElementById('first');div.classList.contains('box'); // truediv.classList.contains('orange'); // false
Hope this code and post will helped you for implement How to check if an element has a class in JavaScript. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs