JQuery - remove all matching classes

I am trying to remove all matching classes that execute a function each()

.

I choose the appropriate class for example [class*="ui-id-"]

. But my below jquery code is not working:

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 relevant class ui-id-

+1


a source to share


5 answers


Try this:

$('#builder [class*="ui-id-"]').removeClass(function(i, j) {
           return j.match(/ui-id-/g).join(" ");
});

      



should remove all matching classes.

+3


a source


The easiest, unfortunately long way, repeat all the 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);
        }
    }
});

      

+2


a source


you don't need every loop here

try it

 jQuery('[class*="ui-id-"]').removeClass('[class*="ui-id-"]');

      

i removed #builder

, it will be easy to figure out the correct answer if you also post the associated HTML

+1


a source


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

      

This will remove all classes starting with ui-id-

present in#builder

+1


a source


Try the following:

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

      

0


a source







All Articles