Each function

Other topics

Basic use

// array
var arr = [
   'one',
   'two',
   'three',
   'four'
];
$.each(arr, function (index, value) {
  console.log(value);
  
  // Will stop running after "three"
  return (value !== 'three');
});
// Outputs: one two three

jQuery each function

HTML:

<ul>
  <li>Mango</li>
  <li>Book</li>
</ul>

Script:

$( "li" ).each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

A message is thus logged for each item in the list:

0: Mango

1: Book

Contributors

Topic Id: 10853

Example Ids: 32540,32544

This site is not affiliated with any of the contributors.