jQuery Traversal - each() Method

Example

Output the text of each li element:

$("button").click(function() {
  $("li").each(function() {
    alert($(this).text());
  });
});

Try It Yourself

Definition and Usage

The each() method specifies a function to be executed for each matching element.

Tip:Returning false can be used to stop the loop early.

Syntax

$().each(function(index, element))
Parameters Description
function(index, element)

Required. Specify the function to be executed for each matching element.

  • index - The index position of the selector
  • element - The current element (also can use the "this" selector)