ash ash - 5 months ago 253
jQuery Question

JQuery removeClass wildcard

Is there any easy way to remove all classes matching, for example,

color-*


so if I have an element:

<div id="hello" class="color-red color-brown foo bar"></div>


after removing, it would be

<div id="hello" class="foo bar"></div>


Thanks!

Answer

The removeClass function takes a function argument since jQuery 1.4.

$("#hello").removeClass (function (index, css) {
    return (css.match (/(^|\s)color-\S+/g) || []).join(' ');
});

Live example: http://jsfiddle.net/xa9xS/973/

Comments