Created
November 13, 2012 15:43
-
-
Save yumyo/4066437 to your computer and use it in GitHub Desktop.
JavaScript toggleClass() function
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
/** | |
* toggleClass() | |
* Expects parameters | |
* elem = DOM element (object, instanceof HTMLElement) | |
* cl = Class name (string) | |
*/ | |
var toggleClass = function(elem, cl) { | |
if (elem.className.indexOf(cl) != -1) { | |
elem.className = elem.className.replace(cl, ''); | |
} else { | |
elem.className += ' ' + cl; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment