It seems odd that grep does not work with an optional selector argument – the existing way you call grep seems to fall outside JQuery conventions. In other words, this works:
$.grep($('div'), function(div) { return div.className == 'section' });
This does not:
$('div').grep(function(div) { return div.className == 'section' });
Furthermore, because of this it becomes tricky to search a list of tags – the first line of code, while it works, returns a flat array instead of a nice chainable JQuery object you can call .css() etc on.
Is there a good reason for grep to work this way in JQuery?
The function you are looking for is the second (callback) variant of
$.fn.filter().It works much the same way as
$.grep, but acts on a jQuery object, and returns a jQuery object of all of the elements that returnedtruefrom the callback function, e.g.: