English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

jQuery each() Method

jQuery Traversal Methods

The each() method traverses the jQuery object and executes a function for each selected element.

The each() method aims to make the DOM loop structure concise and clear, and less prone to errors.

Syntax:

$$(selector).each(function(index, element))

Example

Iterate through each list item and pop up the text of each element:

$("button").click(function(){
  $("li").each(function(){
    alert($(this).text());
  });
});
Test and see‹/›

Used for return false and terminate each() loop:

$("button").click(function(){
  $("div").each(function(index, element){
    $("element").css("backgroundColor", "yellow");
    if($(this).is("#stop")){
      $("span").text("div stop index #" + index);
      return false;
    }
  });
});
Test and see‹/›

Parameter Value

ParameterDescription
function(index, element)Specify the function to be executed for each selected element
  • index-Specify the index position of the selector

  • element-Specify the current element

jQuery Traversal Methods