Skip to content

Instantly share code, notes, and snippets.

@mathiasbynens
Forked from boazsender/jQuery.classList.js
Created July 16, 2010 07:34
Show Gist options
  • Save mathiasbynens/478061 to your computer and use it in GitHub Desktop.
Save mathiasbynens/478061 to your computer and use it in GitHub Desktop.
Fork of boaz’s jQuery.fn.classList() method with some minor improvements
/*
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', value.join(' '));
return this;
} else if (typeof value === 'string') {
return this.attr('class', value);
};
};
return this.attr('class').split(/\s+/);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment