I want remove class, starts with "color". the classes are added dynamically i didn't know many class starts with color.
<div id="sample" class="color1 orange color2 color3 blue"></div>
Jquery
$("#sample").removeClass("[class^='color']");
But it doesn't work. Any Help?
jQuery removeClass() MethodThe removeClass() method removes one or more class names from the selected elements. Note: If no parameter is specified, this method will remove ALL class names from the selected elements.
To replace all existing classes with another class, we can use . attr( "class", "newClass" ) instead. As of jQuery 1.4, the . removeClass() method allows us to indicate the class to be removed by passing in a function.
jQuery removeClass() Method The removeClass() method can remove a single class, multiple classes, or all classes at once from the selected elements.
removeClass() Method. This method removes one or more class names from the selected elements. If no parameter is specified in the removeClass() method, it will remove all class names from the selected elements.
Loop over all the classes and test if they begin with color
.
var classes = $("#sample").attr("class").split(' ');
$.each(classes, function(i, c) {
if (c.indexOf("color") == 0) {
$("#sample").removeClass(c);
}
});
This 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 am getting each word in classes
& replacing anything which starts with color
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With