jQuery: use filter(), but work with both results

Posted by Tomalak on Stack Overflow See other posts from Stack Overflow or by Tomalak
Published on 2010-05-09T17:45:45Z Indexed on 2010/05/09 17:48 UTC
Read the original article Hit count: 225

Filed under:
|

In jQuery, filter() reduces your result to those elements that fulfill a certain condition.

This splits the list in two parts. Working with the "good half" of the elements is easy:

$("some selector").filter(function() {
  // determine result...
  return result;
}).each( /* do something */ );

But how can I work with the "other half" of my elements, too - but without doing the equivalent of this:

$("some selector").filter(function() {
  // determine result...
  return !result;
}).each( /* do something */ );

Basically, I'd like to feed two separate /* do something */ parts to a single filter. One for those that match, and one for the others - without having to select and filter twice.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about filter