Created
December 30, 2015 13:51
-
-
Save elmahdim/9c7a3878654fc899c368 to your computer and use it in GitHub Desktop.
vanilla javascript toggleClass
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 toggleClass(element, className) { | |
if (!element || !className) { return; } | |
var classString = element.className, nameIndex = classString.indexOf(className); | |
if (nameIndex == -1) { | |
classString += (classString.length) ? ' ' + className : className; | |
} else { | |
classString = classString.replace(' '+className, ''); | |
classString = classString.replace(className, ''); | |
} | |
element.className = classString; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment