Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create list of jQuery elements

I have some jquery elements:

var elem1 = $("#elem1");
var elem2 = $("#elem2");
var elem3 = $("#elem3");

I need to create a jquery array (list) of these elements, just like having

var allElements = $("#container").children("div");

How can i do this?

like image 793
Catalin Avatar asked Feb 04 '12 12:02

Catalin


2 Answers

Use .add() method which adds elements to the set of matched elements.

var allElements = elem1.add(elem2).add(elem3);
like image 169
dfsq Avatar answered Nov 11 '22 09:11

dfsq


This worked for me :

var allElements = [];
$("#container > div").each(function(){
    allElements.push($(this));
});

1st create the array, then push each div child of your container in this array.

both $("#container > div") and $("#container").children("div") work
like image 24
Tophe Avatar answered Nov 11 '22 10:11

Tophe