3

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()

2 Answers 2

2

You can use .add:

foo(elem1.add(elem2)); 

To combine the results of both selectors

Sign up to request clarification or add additional context in comments.

3 Comments

+1 how could I forget .add()? Thanks will accept it as soon as I can :)
It's an easy one to forget when you're writing selectors all the time :-)
You see the irony here? My question is being upvoted, but I've reached 200 limit already lol
1

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

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.