Jquery – How to combine two jQuery results


How do you combine two jQuery search results? eg:

var $allFoos = $('.foo'),
    $allBars = $('.bar')
    $allFoosAndBars = $allFoos + $allBars;

Obviously, I just made up that last line, but I hope it makes it sorta clear what I mean. To be clear, the example is greatly simplified, and it could be any arbitrary sets i'm talking about, so $('.foo, .bar') is not what I'm after.

Best Answer

You can use add();

var $foos = $('.foo');

var $foosAndBars = $foos.add('.bar');


var $allFoosAndBars = $allFoos.add($allBars);