Skip to content

Instantly share code, notes, and snippets.

@DenimTornado
Last active August 29, 2015 14:04
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 DenimTornado/df58efbdf2bf18854247 to your computer and use it in GitHub Desktop.
Save DenimTornado/df58efbdf2bf18854247 to your computer and use it in GitHub Desktop.
Adding custom class to an element
var add_class = function(element, classname) {
var class_name = element.className;
//test for existance
if( class_name.indexOf( classname ) !== -1 ) {
return;
}
//add a space if the element already has class
if( class_name !== '' ) {
classname = ' '+classname;
}
element.className = class_name+classname;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment