Check if an element contains a class in JavaScript?
Use element.classList .contains method: element.classList.contains(class); This works on all current browsers and there are polyfills to support older browsers too. Alternatively, if you work with older browsers and don’t want to use polyfills to fix them, using indexOf is correct, but you have to tweak it a little: function hasClass(element, className) { return (‘ ‘ … Read more