views:

62

answers:

3

Hi, I would like to know what $.each() stands for in jquery, What is it selecting?

Is there an equivalent in prototype?

Thanks

+2  A: 

jQuery uses each in two ways:

The .each() method is designed to make DOM looping constructs concise and less error-prone. When called it iterates over the DOM elements that are part of the jQuery object. Each time the callback runs, it is passed the current loop iteration, beginning from 0. More importantly, the callback is fired in the context of the current DOM element, so the keyword this refers to the element.

http://api.jquery.com/each/

The $.each() function is not the same as .each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword.)

http://api.jquery.com/jQuery.each/

Barlow Tucker
this is actually a different function from the one being asked about
cobbal
@cobbal Are you sure?
DavidYell
@David - @cobbal is correct. The `.each()` method is a method that is part of every jQuery object used specifically to iterate over the matched set. `jQuery.each()` is a utility to iterate over any collection. The OP is asking specifically about `jQuery.each()`.
patrick dw
You guys are correct, I have updated my answer
Barlow Tucker
Ah ok. Every day is a school day!
DavidYell
+4  A: 

I think you should rather look at

jQuery.each()

From the documentation

The $.each() function is not the same as .each(), which is used to iterate, exclusively, over a jQuery object. The $.each() function can be used to iterate over any collection, whether it is a map (JavaScript object) or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time. (The value can also be accessed through the this keyword.)

astander
+1  A: 

$.each() isn't selecting anything. It is just a utility to iterate over a collection.

When you do:

$('someSelector').each(function() {
    // do something
});

jQuery is internally calling:

jQuery.each( this, callback, args );

...with this representing the matched set.

http://github.com/jquery/jquery/blob/master/src/core.js#L231

You could just as easily call it yourself manually.

jQuery.each( $('someSelector'), function() {
    // do something
});
patrick dw