user007
user007

Reputation: 3243

jQuery - remove all matching classes

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

Answers (5)

loxxy
loxxy

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

Ja͢ck
Ja͢ck

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

Yogesh
Yogesh

Reputation: 924

jQuery("#builder [class^='ui-id-']").removeClass();

This will remove all the classes starts wiht ui-id- present in #builder

Upvotes: 1

Manoj Yadav
Manoj Yadav

Reputation: 6612

Try this:

jQuery('.ui-id-').removeClass('ui-id-');

Upvotes: 0

bipen
bipen

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

Related Questions