Skip to content

Instantly share code, notes, and snippets.

@louisremi
Created July 29, 2010 20:19
Show Gist options
  • Save louisremi/499124 to your computer and use it in GitHub Desktop.
Save louisremi/499124 to your computer and use it in GitHub Desktop.
// Minimalist class (className) manipulation library
var klass = {
has: function(elem, name) {
return (' '+elem.className+' ').indexOf(' '+name+' ') != -1;
},
add: function(elem, name) {
if(!klass.has(elem, name)) {
elem.className += ' '+name;
}
},
remove: function(elem, name) {
elem.className = (' '+elem.className+' ').split(' '+name+' ').join(' ').slice(1, -1);
},
toggle: function(elem, name) {
klass[klass.has(elem, name)? 'remove' : 'add'](elem, name);
}
}
@louisremi
Copy link
Author

more triming, more caching, smaller size once closure-compiled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment