What is the difference between the following:
1. var el = $('.test');
2. var el_array = [$('.test')];
alert(el); // output [object Object]
alert(el_array); // same output as above
I really do not understand how it works.
What is the difference between the following:
1. var el = $('.test');
2. var el_array = [$('.test')];
alert(el); // output [object Object]
alert(el_array); // same output as above
I really do not understand how it works.
The $('.test')
expression returns a jQuery object, which is an array-like object containing references to DOM element objects.
They are displayed the same because both are arrays (or array like objects) that contains some kind of object. The browser just doesn't descibe them detailed enough that you can see the difference.
To descibe the content of the variables more precisely (assuming that the .test
selector finds three elements):
el: jQuery[ Element, Element, Element ]
el_array: Array[ jQuery[ Element, Element, Element ] ]