tags:

views:

52

answers:

3

Hi!

Can anybody tell me, if I call ex.

var obj1 = $('#element_id')
var obj2 = $('#element_id')

will obj1 refer to the same javascript object as obj2 does, or there will be 2 different objects, having the same HTML element they wrap?

And what about complex selectors, like ('.my_class, .my-class2'), which wrap collection of objects?

+1  A: 

They will be different objects.

It's very easy for you to test yourself:

var obj1 = $('#test');
var obj2 = $('#test');
var array = new Array();
array[0] = obj1;
alert(obj1 == array[0]);
alert(obj1 == obj2);

The first alert will be "true". The second will be "false".

cbp
+1  A: 

They are different, that's why it is a common practice to store your jQuery objects, you can reduce selector parsing, DOM tranversal etc...

CMS
A: 

Are there any opportunities to get the first object having only selector? this is needed when I store some values in that object, and want to read them from another part of the code.

glaz666