Skip to content

Instantly share code, notes, and snippets.

@aispin
Forked from jakubp/gist:2881585
Last active December 20, 2015 20:39
Show Gist options
  • Save aispin/6191743 to your computer and use it in GitHub Desktop.
Save aispin/6191743 to your computer and use it in GitHub Desktop.
Remove classes that have given prefix
/*
* Remove classes that have given prefix
* Example:
* You have an element with classes "apple juiceSmall juiceBig banana"
* You run:
* $elem.removeClassByPrefix('juice');
* The resulting classes are "apple banana"
* NOTE: discussion of implementation techniques for this, including why simple RegExp with word boundaries isn't correct:
* http://stackoverflow.com/questions/57812/jquery-remove-all-classes-that-begin-with-a-certain-string#comment14232343_58533
* https://gist.github.com/mamboer/6191743
*/
(function ( $ ) {
$.fn.removeClassByPrefix = function (prefix) {
this.each( function ( i, it ) {
var classes = it.className.split(" ").map(function (item) {
return item.indexOf(prefix) === 0 ? "" : item;
});
it.className = classes.join(" ");
});
return this;
}
})( jQuery );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment