Created
January 17, 2019 16:17
-
-
Save dougwollison/713d4364fe00d1606f1829486edb74ec to your computer and use it in GitHub Desktop.
jQuery removeClass regex support
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
( 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