OK, this is quite a extensive overview of the jQuery. each () function. This is one of the jQuery's most important and most used functions so that's the reason why I ve chosen to go into Su CH detail about it and really get down and dirty on how to use it to it's full useful potential!
Firstly, what's jQuery. each ()
Basically, the jquery. each () function was 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
| 123456789101112131415161718192021 |
//DOM ELEMENTS$("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 //ARRAYSvar arr = [ "one", "two", "three", "four", "five" ];jQuery.each(arr, function(index, value) { console.log(this); return (this != "three"); // will stop running after "three" });//outputs: one two three//OBJECTSvar obj = { one:1, two:2, three:3, four:4, five:5 }; jQuery.each(obj, function(i, val) { console.log(val); });//outputs: 1 2 3 4 5 |
For a further advanced Jquery.each example see Create Text excerpts for paragraphs on your Web page.
1. Basic Jquery.each () Function Example
| 12345678910111213 |
$(‘a‘).each(function(index, value){ console.log($(this).attr(‘href‘));});//outputs: every links href element on your web page$(‘a‘).each(function(index, value){ var ihref = $(this).attr(‘href‘); if (ihref.indexOf("http") >= 0) { console.log(ihref+‘<br />‘); }});//outputs: every external href on your web page |
Eg–if had the following links anywhere on your Web page:
| 123 |
< a href = "HTTP +/ Www.jquery4u.com " >jquery4u</ a Code class= "XML Plain" >> < a href = "HTTP +/ Www.phpscripts4u.com " >php4u</ a Code class= "XML Plain" >> < a href = "http://www.blogoola.com " >blogoola</ a > |
It would output:
| 1234567 |
http://jquery4u.comhttp://www.phpscripts4u.comhttp://www.blogoola.com |
2. Jquery.each () Array Example
| 12345 |
varnumberArray = [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
| 123456789101112131415 |
(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})(jQuery); |
Also See Example JSON Files.
4. Jquery.each () Class Example
This example shows all-to-loop through each element with a 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 H Tmldivelement] It should help clear it up.
| 123 |
< class = " ProductDescription " >red</ div > < div class = Code class= "XML plain" >>orange</ div > < div class = "ProductDescription" >green</ div > |
| 1234 |
$.each($(‘.productDescription‘), function(index, value) { console.log(index + ‘:‘+ value); });//outputs: 1:Red 2:Orange 3:Green |
You don't have the to include index and value these is just parameters which help determine which DOM element your currently Iterating. You could also write it like this:
| 1234 |
$.each($(‘.productDescription‘), function() { console.log($(this).html());});//outputs: Red Orange Green |
5. Jquery.each () Delay Example
Click to see it in action
This is awesome! Look in the menu at the top as this contains the first list items on the page!.
| 12345678 |
jQuery(‘#5demo‘).bind(‘click‘, function(e){ jQuery(‘li‘).each(function(i) { jQuery(this).css("background-color","orange"); jQuery(this).delay(i*200).fadeOut(1500); });}); |
Conclusion
Make use of the, each function as much as can because it ' s fast and would save you heaps of time!
Remember: $.each () and $ (selector). each () is a different methods defined in both different ways, one with Jquer Y.each = function and the other with Jquery.fn.each = function.
Note: the Console.log () commands is just for use with Firebug.
5 Jquery.each () Function Examples