Skip to content

Instantly share code, notes, and snippets.

@pallavizanje
Forked from sonnyt/gist:8585696
Created November 16, 2016 10:55
Show Gist options
  • Select an option

  • Save pallavizanje/6e188521c4c800a3ce7c38b58ec799ff to your computer and use it in GitHub Desktop.

Select an option

Save pallavizanje/6e188521c4c800a3ce7c38b58ec799ff to your computer and use it in GitHub Desktop.
JavaScript Check If Element Has Class
function hasClass(element, className) {
return element.className && new RegExp("(^|\\s)" + className + "(\\s|$)").test(element.className);
}
var myDiv = document.getElementById('MyDiv');
hasClass(myDiv, 'active');
// OR
Element.prototype.hasClass = function(className) {
return this.className && new RegExp("(^|\\s)" + className + "(\\s|$)").test(this.className);
};
document.getElementById('MyDiv').hasClass('active');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment