Skip to content

Instantly share code, notes, and snippets.

@goodpixels
Last active September 16, 2015 16:39
Show Gist options
  • Save goodpixels/da1df0dcd102806fcf0f to your computer and use it in GitHub Desktop.
Save goodpixels/da1df0dcd102806fcf0f to your computer and use it in GitHub Desktop.
Remove all classes except for the ones given.
/*! (c) 2015 John Przeslakowski | Code released under the MIT license | goodpixels.co.uk */
( function ( $ ) {
$.fn.removeClassExcept = function ( val ) {
return this.each( function( index, el ) {
var keep = val.split( ' ' );
var reAdd = [];
var $el = $(el);
for( var c = 0; c < keep.length; c++ ) {
if( $el.hasClass( keep[ c ] ) ) reAdd.push( keep[ c ] );
}
$el.removeClass().addClass( reAdd.join( ' ' ) );
} );
};
}( jQuery ) );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment