Skip to content

Instantly share code, notes, and snippets.

@Farmatique
Last active June 22, 2018 14:41
Show Gist options
  • Save Farmatique/d5756faf782b784d273b02a18257ed1c to your computer and use it in GitHub Desktop.
Save Farmatique/d5756faf782b784d273b02a18257ed1c to your computer and use it in GitHub Desktop.
Pure JS Class helpers addclass, removeclass
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