How to remove a specific class starts with "color" in jquery

I want to remove a class starting with "color". classes are added dynamically, I did not know many starts of classes with color.

<div id="sample" class="color1 orange color2 color3 blue"></div> 

Jquery

 $("#sample").removeClass("[class^='color']"); 

But that will not work. Any help?

+7
jquery removeclass
source share
2 answers

Scroll through all the classes and see if they start with color .

 var classes = $("#sample").attr("class").split(' '); $.each(classes, function(i, c) { if (c.indexOf("color") == 0) { $("#sample").removeClass(c); } }); 
+9
source share

It will work here

 $('div')[0].className = $('div')[0].className.replace(/\bcolor.*?\b/g, ''); 

OR

  $('div').attr('class',$('div').attr('class').replace(/\bcolor.*?\b/g, '')); 

Basically here I get every word in classes and replace everything that starts with color .

+7
source share

All Articles