Created
March 22, 2013 13:30
-
-
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`.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.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