5 jQuery.each() Function Examples

OK, this is quite an extensive overview of the jQuery .each() function. This is one of jQuery’s most important and most used functions so that’s the reason why I’ve chosen to go into such detail about it and really get down and dirty about how to use it to it’s full useful potential!

Firstly, what is jQuery .each()

Basically, the jQuery .each() function is used to loop through each element of the target jQuery object. Very useful for multi element DOM manipulation, looping arrays and object properties.

jQuery .each() Syntax

$("div").each(function(index, value) { 
    console.log('div' + index + ':' + $(this).attr('id')); 
//outputs the ids of every div on the web page
//ie - div1:header, div2:body, div3:footer

var arr = [ "one", "two", "three", "four", "five" ];
jQuery.each(arr, function(index, value) {
       return (this != "three"); // will stop running after "three"
//outputs: one two three

var obj = { one:1, two:2, three:3, four:4, five:5 };
    jQuery.each(obj, function(i, val) {
//outputs: 1 2 3 4 5

For a more advanced jQuery.each example see Create Text Excerpts for Paragraphs on your web page.

1. Basic jQuery.each() Function Example

$('a').each(function(index, value){
//outputs: every links href element on your web page

$('a').each(function(index, value){
	var ihref = $(this).attr('href');
	if (ihref.indexOf("http") >= 0) 
'); } }); //outputs: every external href on your web page

eg – if you had the following links anywhere on your web page:


It would output:


2. jQuery.each() Array Example

var numberArray = [0,1,2,3,4,5];
jQuery.each(numberArray , function(index, value){
     console.log(index + ':' + value); 
//outputs: 1:1 2:2 3:3 4:4 5:5

3. jQuery.each() JSON Example

(function($) {
var json = [ 
    { "red": "#f00" },
    { "green": "#0f0" },
    { "blue": "#00f" }

$.each(json, function() {
  $.each(this, function(name, value) {
    /// do stuff
    console.log(name + '=' + value);
//outputs: red=#f00 green=#0f0 blue=#00f

Also see 10 Example JSON Files.

4. jQuery.each() Class Example

This example shows you how to loop through each element with class=”productDescription” given in the HTML below.

Update: 19/03/13 – This jsfiddle shows you the object types returned by $.each() if your confused about [object HTMLDivElement] it should help clear it up.

$.each($('.productDescription'), function(index, value) { 
    console.log(index + ':' + value); 
//outputs: 1:Red 2:Orange 3:Green

You don’t have to include index and value these are just parameters which help determine which DOM element your currently iterating. You could also write it like this:

$.each($('.productDescription'), function() { 
//outputs: Red Orange Green

5. jQuery.each() Delay Example

Click to see it in action
This is awesome! Look up at the menu at the top as this contains the first list items on the page!.

jQuery('#5demo').bind('click', function(e)


Make use of the .each function as much as you can because it’s fast and will save you heaps of time!

Remember: $.each() and $(selector).each() are two different methods defined in two different ways, one with jQuery.each = function and the other with jquery.fn.each = function.

Note: the console.log() commands are just for use with firebug.

jQuery Each Function Examples