-
-
Save khoerodin/fb7ab7d7c892243622c6b8dc6f9ad5dc to your computer and use it in GitHub Desktop.
JavaScript Check If Element Has Class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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