jQuery: use 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/06/11 18:22 UTC
Read the original article Hit count: 187

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 else */ );

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 filter twice. Am I missing a jQuery function that does this?


P.S.: I guess I could do:

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

But I was hoping for something nicer.

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about filter