Skip to content

Instantly share code, notes, and snippets.

@rwaldron
Forked from boazsender/jQuery.classList.js
Created July 16, 2010 15:21
Show Gist options
  • Save rwaldron/478484 to your computer and use it in GitHub Desktop.
Save rwaldron/478484 to your computer and use it in GitHub Desktop.
/*
Usages:
$(selector).classList() //returns an array of classnames
$(selector).classList('newclass') //replaces the current element's classes
$(selector).classList(['new', 'class', 'names']) //replaces the current element's classes
*/
jQuery.fn.extend({
classList: function( value ) {
if( value ){
if( jQuery.isArray(value)){
this.attr('class', '')
for(var i in value){
this.addClass(value[i])
}
return this;
}
if( typeof value == 'string'){
this.attr('class', '').addClass(value);
return this;
}
}
return this.attr('class').split(/\s+/)
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment