Skip to content

Instantly share code, notes, and snippets.

@yumyo
Created November 13, 2012 15:43
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 yumyo/4066437 to your computer and use it in GitHub Desktop.
Save yumyo/4066437 to your computer and use it in GitHub Desktop.
JavaScript toggleClass() function
/**
* 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