Skip to content

Instantly share code, notes, and snippets.

@dougwollison
Created January 17, 2019 16:17
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 dougwollison/713d4364fe00d1606f1829486edb74ec to your computer and use it in GitHub Desktop.
Save dougwollison/713d4364fe00d1606f1829486edb74ec to your computer and use it in GitHub Desktop.
jQuery removeClass regex support
( function( $ ) {
// Store the original method for all other uses
var oldRemoveClass = $.fn.removeClass;
// Replace existing method iwth new version
$.fn.removeClass = function( className ) {
if ( className instanceof RegExp ) {
// Run a regex replace on each element's class name list
return $( this ).each( function() {
var classes = $( this ).attr( 'class' ) || '';
classes = classes.replace( className, '' ).trim(); // For excess spaces
$( this ).attr( 'class', classes );
} );
} else {
// Use original method
return oldRemoveClass.apply( this, arguments );
}
};
} )( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment