KOVIKO
KOVIKO

Reputation: 1379

Is there a way to perform .filter and .find at the same time?

I am dealing with templates and finding certain elements for future updating when responding to events. However, I'd like the ability to mark these elements with a class, data-* attribute, or something else without having to know the markup beforehand.

Is there a way to perform a .filter() and a .find() search at the same time?

Upvotes: 1

Views: 78

Answers (2)

Shelvacu
Shelvacu

Reputation: 4362

Simple extension for this purpose:

jQuery.fn.findIn = function(selector){
  return this.filter(selector).add(this.find(selector));
}

Then simply call .findIn('.your-selector').

currently taking suggestions for a better name.

Upvotes: 0

KOVIKO
KOVIKO

Reputation: 1379

My current solution is to use an .add() operation to combine the two results, but this seems a bit convoluted.

elements.filter(selector).add(elements.find(selector))

Upvotes: 1

Related Questions