Merge jQuery objects

Possible duplicate:
Merging jQuery objects

See demo

a = $('div:eq(0)'); b = $('div:eq(1)'); c = $('div:eq(2)'); d = $('div:eq(3)'); e = $('div:eq(4)'); f = $('div:eq(5)'); console.log($([a,b,c,d,e,f]).find('a')) 

What I'm trying to do is add multiple jQuery objects to one set.

I know that I can add them one at a time using .add() , but I'm looking for a way to turn an array of jQuery objects into a single jQuery set.

Is there any way to do this?

+4
source share
2 answers

Not possible in plain jQuery, since the syntax is already used for DOM elements.

But a simple plugin will do the job perfectly:

http://jsfiddle.net/gMDjZ/

 a = $('div:eq(0)'); b = $('div:eq(1)'); c = $('div:eq(2)'); d = $('div:eq(3)'); e = $('div:eq(4)'); f = $('div:eq(5)'); jQuery.fromArray = function(a) { var c = jQuery(); for(x in a) { c = c.add(a[x]); } return c; } console.log($.fromArray([a,b,c,d,e,f]).find('a')) 

+3
source

You are missing : in your code. Write it like -

 a = $('div:eq(0)'); 

Use jQuery.merge() http://api.jquery.com/jQuery.merge/

or jQuery.map() http://api.jquery.com/jQuery.map/

+1
source

All Articles