Skip to content

Instantly share code, notes, and snippets.

@andreiglingeanu
Created February 17, 2013 16:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiglingeanu/4972049 to your computer and use it in GitHub Desktop.
Save andreiglingeanu/4972049 to your computer and use it in GitHub Desktop.
javascript class utility
function addClass(el, cls) {
var c = el.className.split(' ');
for (var i=0; i<c.length; i++) {
if (c[i] == cls) return;
}
c.push(cls);
el.className = c.join(' ');
}
function removeClass(el, cls) {
var c = el.className.split(' ');
for (var i=0; i<c.length; i++) {
if (c[i] == cls) c.splice(i--, 1);
}
el.className = c.join(' ');
}
function hasClass(el, cls) {
for (var c = el.className.split(' '),i=c.length-1; i>=0; i--) {
if (c[i] == cls) return true;
}
return false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment