Learn jQuery

jQuery.each()

jQuery.each() function is used to iterate an object and an array.jQuery.each() function is not the same as $(selector).each(),$(selector).each() is used to iterate jQuery object.

Syntax

jQuery.each(object,callback )

jQuery.each() takes 2 parameters.First is the object over which the iteration is to be performed and second is the callback function.

Example

Iterate an javascript array

HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<title>jQuery.each() Example </title>
</head>
<button>Iterate an Js Array</button>
<div></div>
</html>
Javascript
$(document).ready(function(){
$("button").click(function(){
var myJsArray = [10,20,30,40,50,60,70];
jQuery.each(myJsArray, function(index, value)
{
$("div").append('element at index ' + index + ' is ' + value + "
"); }); }); });

Iterate an javascript object

HTML
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script>
<title>jQuery.each() Example </title>
</head>
<button>Iterate an Js Object</button>
<div></div>
</html>
Javascript
$(document).ready(function(){
$("button").click(function(){
var myJsObj = {"Google" : "http://google.com","Tutsway" : "http://tutsway.com"};
jQuery.each(myJsObj, function(key, key)
{
$("div").append('value for key ' + key + ' is ' + value+ "
"); }); }); });