Skip to content

Instantly share code, notes, and snippets.

@zaus
Created March 22, 2013 13:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zaus/5221271 to your computer and use it in GitHub Desktop.
Save zaus/5221271 to your computer and use it in GitHub Desktop.
The missing jQuery function to easily remove classes matching a filter -- combines `$el.removeClass` and `$.grep`.
$.fn.removeClassWhere = function (filter) {
/// <summary>
/// Remove classes matching a filter fn(class, index_in_list) -- combines .removeClass and .grep
/// <example>$('.things').removeClassWhere(function (cl) { return -1 != cl.indexOf(matches); });</example>
/// </summary>
return $(this).removeClass(function (i, classes) {
return $.grep(classes.split(' '), filter).join(' ');
});
};//-- fn removeClassWhere
$.fn.removeClassWith = function (matches) {
/// <summary>
/// Remove classes matching a value -- combines .removeClass and .grep
/// <example>$('.things').removeClassWith('something-you-dont-want')</example>
/// </summary>
if (!$.isArray(matches)) {
return $(this).removeClassWhere(function (cl) {
return -1 != cl.indexOf(matches);
});
}
return $(this).removeClass(function (i, classes) {
// http://stackoverflow.com/a/9930447/1037948
return $.grep(classes.split(' '), function (cl) { return $.inArray(cl, matches) < 0; }).join(' ');
});
};//-- fn removeClassWhere
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment