Selector $('.class')
is not returning an array of objects, but "just" objects.
It returns a jQuery object. A jQuery object can be seen as a collection of HTML elements.
As a jQuery object supports the .length
property and the []
accessor, it could be seen as an array of HTML Elements, though it is really not.
Considering that this is not an array of objects => it is not possible to iterate through all found objects with for..in loop?
Using the .length
and []
directly in the $('.my_class')
, you can use for
loops.
But don't use for..in
, as it iterates through the properties of a JavaScript object, not through the indexes of an array.
Is .each()
the only solution?
Being a "solution", of course, depends on the problem. But if you are asking about alternatives, if you must use the for
loop, you could:
var elements = $('.my_class'), i, n, element;
for (i = 0, n = elements.length; i < n; i++) {
element = elements[i];
// do stuff with element (it is an HTML Element)
}
Though, if you really need an array of elements, you can call the .get()
or, better, the .toArray()
function in the jQuery object:
// here, elementsArray is really an array of HTML Elements
var elementsArray = $('.my_class').toArray();