Skip to content

Instantly share code, notes, and snippets.

@alvin2ye
Forked from cowboy/jQuery.classList.js
Created July 17, 2010 01:44
Show Gist options
  • Save alvin2ye/479157 to your computer and use it in GitHub Desktop.
Save alvin2ye/479157 to your computer and use it in GitHub Desktop.
jQuery.classList.js
/*
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.classList = function( classNames ) {
if ( jQuery.isArray( classNames ) ) {
// An array was passed, join it into a string.
classNames = classNames.join(' ')
} else if ( classNames ) {
// Individual arguments were passed, join them into a string.
classNames = Array.prototype.join.call( arguments, ' ' );
} else {
// No arguments were passed, return an array of class names.
return this.attr( 'class' ).split( /\s+/ );
}
// Set class names and return the original jQuery object.
return this.attr( 'class', classNames );
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment