Last active
February 9, 2021 04:20
-
-
Save bergantine/1165817 to your computer and use it in GitHub Desktop.
JavaScript hasClass(), addClass(), removeClass(). #javascript
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
// class manipulation from http://www.openjs.com/scripts/dom/class_manipulation.php | |
function hasClass(ele,cls) { | |
return ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')); | |
} | |
function addClass(ele,cls) { | |
if (!this.hasClass(ele,cls)) ele.className += " "+cls; | |
} | |
function removeClass(ele,cls) { | |
if (hasClass(ele,cls)) { | |
var reg = new RegExp('(\\s|^)'+cls+'(\\s|$)'); | |
ele.className=ele.className.replace(reg,' '); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Old one but just stumbled across this one and I notice that you have
this.hasClass
in theaddClass
function.