JQuery has two methods each . One of them is designed to cycle through a jQuery object that contains many matches. For example, suppose we wanted to find all the paragraphs on a page:
$("p").each(function(){
Secondly, there is a more general each for iterating over objects or arrays:
var names = ["Jonathan", "Sampson"]; $.each(names, function(){
When jQuery cycles through the elements in any of these examples, it counts which object it processes. When it performs our anonymous function, it passes two parameters: the current value that we find (index), and this object (value).
var names = ["Jonathan", "Sampson"]; $.each(names, function(index, value){ alert( value + " is " + index ); });
Which outputs are "Jonathan is 0" and "Sampson is 1", since we use an index based on zero.
But what about our own jQuery object?
$("p").each(function(index, value){ alert( value.textContent );
In this case, value is the actual HTMLParagraphElement object, so we can access properties like textContent or innerText if you want:
Sampson
source share