jQuery Traversing - 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 run 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 run for each matching element.

  • index - Selector index position
  • element - Current element (also can use "this" selector)