ash ash - 1 year ago 570
jQuery Question

JQuery removeClass wildcard

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


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>


Answer Source

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:

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download