loop through elements in an array

Description

.each() works with objects, array-like objects, and arrays.

$.each() is zero-based and accepts two arguments: the collection to iterate over and a callback function.

The callback function accepts the arguments: the index currently operated on and the element value.

Example

The following code illustrates using $.each() to iterate over the members of a simple array:


<!DOCTYPE html>
<html>
  <head>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript">     
      $.ready(function(){<!-- w  w  w. ja  va2 s . co  m-->
          var numArray = [1,2,3,4];
          $.each( numArray, function( index, value ){
              value = value * 2;
              console.log( "index is:"+index+" new value is:" + value );
          });          
      });
    </script>
  </head>
  <body>  
  </body>
</html>

Click to view the demo





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities