Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two jQuery results

Tags:

jquery

People also ask

How to concat array in jQuery?

The jQuery merge() method together merges the content of two arrays into the first array. This method returns the merged array. The merge() method forms an array containing the elements of both arrays. If we require the first array, we should copy it before calling the merge() method.

Can you combine jQuery and JavaScript?

It is possible to run jQuery and JavaScript together in you application. All you need to know is, which is a JavaScript Object when trying to run some methods of it. jQuery can be handy sometimes. But, yes!

Can we use multiple selectors in jQuery?

You can specify any number of selectors to combine into a single result. This multiple expression combinator is an efficient way to select disparate elements. The order of the DOM elements in the returned jQuery object may not be identical, as they will be in document order.


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