Skip to content

Instantly share code, notes, and snippets.

@jboesch
Created January 12, 2011 17:23
Show Gist options
  • Save jboesch/776500 to your computer and use it in GitHub Desktop.
Save jboesch/776500 to your computer and use it in GitHub Desktop.
Allows you to removeClasses based on a wildcard
// http://jsfiddle.net/w5TnB/5/
// HTML
<div class="myclass ui-dialog ui-widget ui-blah something">
<a href="#">REMOVE ALL CLASSES WITH UI PREFIX</a>
</div>
// Regular jQuery
$('a').click(function(){
$(this).parent().removeClass('ui-* something');
return false;
});
// My new removeClass that handles classes ending in *
$.fn.removeClass = (function(_oldRemoveClass){
return function(){
// Turn arguments into a true array
var args = Array.prototype.slice.call(arguments);
var class_list = args[0];
var classes = class_list.split(' ');
var new_class_list = [];
var get_rid = [];
var $t = $(this);
for(var i = 0, clen = classes.length; i < clen; i++){
if(classes[i].indexOf('*') !== -1){
var c_stripped = classes[i].replace('*', '');
$t.filter('[class*="' + c_stripped + '"]').each(function(){
var cl = this.className.split(' ');
for(var j = 0, jlen = cl.length; j < jlen; j++){
if(cl[j].indexOf(c_stripped) !== -1){
get_rid.push(cl[j]);
}
};
});
}
else {
new_class_list.push(classes[i]);
}
}
args[0] = $.merge(new_class_list, get_rid).join(' ');
return _oldRemoveClass.apply(this, args);
};
})($.fn.removeClass);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment