Created
March 8, 2013 01:02
-
-
Save srawlins/5113405 to your computer and use it in GitHub Desktop.
Even if you don't have jQuery available, you can use the add and removeClass() functions!
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
| function jQaddClass( elem, value ) { | |
| var newClass = value; | |
| if ( elem.nodeType === 1 ) { | |
| if ( !elem.className ) { | |
| elem.className = value; | |
| } else { | |
| var className = " " + elem.className + " ", setClass = elem.className; | |
| if ( className.indexOf( " " + newClass + " " ) < 0 ) { | |
| setClass += " " + newClass; | |
| } | |
| elem.className = setClass.replace(/^ */, "").replace(/ *$/, ""); | |
| } | |
| } | |
| } | |
| function jQremoveClass( elem, value ) { | |
| var oldClass = value; | |
| var rclass = /[\n\t\r]/g; //from jQuery's attributes.js | |
| if ( elem.nodeType === 1 && elem.className ) { | |
| if ( value ) { | |
| var className = (" " + elem.className + " ").replace(rclass, " "); | |
| className = className.replace(" " + oldClass + " ", " "); | |
| elem.className = className.replace(/^ */, "").replace(/ *$/, ""); | |
| } else { | |
| elem.className = ""; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment