Amit Joki
Amit Joki

Reputation: 59252

How can I wrap two jquery objects into one?

I have two functions.

1

function foo(elems){
   elems.each(function(){

   });
}
foo($('selector1,selector2')); // calling foo

2

function bar(elem1,elem2){
  // some logic 
}
bar($('selector1'),$('selector2)); // calling bar

However, now I need to use foo() inside bar().

function bar(elem1,elem2){
   foo(?); // I'm stuck here. I want to pass both elem1 and elem2 as single jquery object
}

So I tried $(elem1,elem2) but it only includes elem1. So what should I do, so that I can pass elem1 and elem2 as a single jquery object which I'll iterate over in foo() by .each()

Upvotes: 3

Views: 66

Answers (2)

attila
attila

Reputation: 2219

You can use the jQuery "add" method, like this

$(elem1).add(elem2); //this now contains both elements

Upvotes: 1

CodingIntrigue
CodingIntrigue

Reputation: 78545

You can use .add:

foo(elem1.add(elem2));

To combine the results of both selectors

Upvotes: 2

Related Questions