Skip to content

Instantly share code, notes, and snippets.

@pyshchyk-o
Forked from MakingJamie/has_class.js
Created February 20, 2016 16:20
Show Gist options
  • Select an option

  • Save pyshchyk-o/2d4284551a1c02c13383 to your computer and use it in GitHub Desktop.

Select an option

Save pyshchyk-o/2d4284551a1c02c13383 to your computer and use it in GitHub Desktop.
has class function - vanilla JS. Check if element has specified class
// hasClass, takes two params: element and classname
function hasClass(el, cls) {
return el.className && new RegExp("(\\s|^)" + cls + "(\\s|$)").test(el.className);
}
/* use like below */
// Check if an element has class "foo"
if (hasClass(element, "foo")) {
// Show an alert message if it does
alert("Element has the class!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment