Reputation: 3243
I am trying to remove all matching classes running each()
function.
I am selecting matching class like [class*="ui-id-"]
. But my below jquery code wont work:
My buggy code:
jQuery('#builder [class*="ui-id-"]').each(function(){
jQuery(this).removeClass('[class*="ui-id-"]');
});
Please correct my code so that it can remove all matching class ui-id-
Upvotes: 1
Views: 1303
Reputation: 13151
Try it this way:
$('#builder [class*="ui-id-"]').removeClass(function(i, j) {
return j.match(/ui-id-/g).join(" ");
});
should remove all the matched classes.
Upvotes: 4
Reputation: 173562
The easiest would unfortunately be the big way, iterate all elements:
jQuery('#builder').find('*').each(function() {
var classes = this.className.split(/\s+/);
$.each(classes, function(i, c) {
if (c.indexOf('ui-id-') === 0) {
$(this).removeClass(c);
}
}
});
Upvotes: 2
Reputation: 924
jQuery("#builder [class^='ui-id-']").removeClass();
This will remove all the classes starts wiht ui-id-
present in #builder
Upvotes: 1
Reputation: 36531
you don't need each loop here
try this
jQuery('[class*="ui-id-"]').removeClass('[class*="ui-id-"]');
i removed the #builder
, it will be easy to figure out the correct answer if you post you related html code too
Upvotes: 1