jQuery

Each function

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


This modified text is an extract of the original Stack Overflow Documentation created by the contributors and released under CC BY-SA 3.0 This website is not affiliated with Stack Overflow