Last active
June 22, 2018 14:41
-
-
Save Farmatique/d5756faf782b784d273b02a18257ed1c to your computer and use it in GitHub Desktop.
Pure JS Class helpers addclass, removeclass
This file contains hidden or 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(ele,cls) { | |
return !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)')); | |
} | |
function addClass(ele,cls) { | |
if (!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