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.
You can use add();
var $foos = $('.foo');
var $foosAndBars = $foos.add('.bar');
or
var $allFoosAndBars = $allFoos.add($allBars);
Another solution is to use jQuery.merge() (jQuery > 1.0)
Description: Merge the contents of two arrays together into the first array.
So you could simply use it to merge both result :
var $allFoos = $('.foo');
var $allBars = $('.bar');
var $allFoosAndBars = $.merge($allFoos, $allBars);