Skip to content

Instantly share code, notes, and snippets.

@ethertank
Created February 12, 2013 13:48
Show Gist options
  • Save ethertank/4769998 to your computer and use it in GitHub Desktop.
Save ethertank/4769998 to your computer and use it in GitHub Desktop.
hasClass / addClass / removeClass
// http://www.avoid.org/?p=78
function hasClass(el, name) {
return new RegExp('(\\s|^)' + name + '(\\s|$)').test(el.className);
}
function addClass(el, name) {
if ( !hasClass(el, name) )
el.className += (el.className ? ' ' : '') + name;
}
function removeClass(el, name) {
if ( hasClass(el, name) )
el.className = el.className.replace(new RegExp('(\\s|^)' + name + '(\\s|$)'), ' ').replace(/^\s+|\s+$/g, '');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment