The each method

The each method calls the given function for each element of the set. Unlike the similar map method, which returns a newly formed set of different elements and objects, the each method returns the original set of elements.

Syntax

The elements themselves are available in the function, in the variable this, and their ordinal numbers in the set — in the variable index:

javascript$(selector).each(function(index, elem));

Example

Let's display the contents of each paragraph sequentially using the each and text methods:

html<p>text1</p>
<p>text2</p>
<p>text3</p>
javascript$('.www').each(
  function() {
    console.log($(this).text()); // will bring out 'text1', 'text2' And and 'text3' 
  }
);

See also

  • method filter,
    which filters elements by a specific selector
  • method map,
    which calls a given function for each element of a set