Created
February 17, 2013 16:14
-
-
Save andreiglingeanu/4972049 to your computer and use it in GitHub Desktop.
javascript class utility
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
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