jQuery .each() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.each(function) (since jQuery 1.0) runs a callback once for every matched element. Inside the callback, this is the current DOM element, and the first argument is the zero-based index.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// Official pattern
$("li").each(function (index) {
  console.log(index + ": " + $(this).text());
});

// this = DOM element | $(this) = jQuery wrapper
// return false → break the loop early

// Often unnecessary (implicit iteration):
$("li").addClass("foo");
CallWhat it does
.each()Learn jQuery .each(): iterate matched elements with a callback, use index and this, wrap with $(this), break early with return false, and know when implicit iteration is enough.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the elements — e.g. $("li").
Step 2Call .each(function (index, element) { … }).
Step 3Use this or element for the current DOM node.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Official Index + Text Log

Build a line for each list item (official intro pattern, CSS-free).

jQuery
var lines = [];
$("li").each(function (index) {
  lines.push(index + ": " + $(this).text());
});
$("#out").text(lines.join("\n"));
Try It Yourself

How It Works

  1. Select. $("li") builds the matched set.
  2. Loop. The callback runs once per item.
  3. Index. Starts at 0 for the first element.

Example 2 — this vs $(this)

Same element — native id vs jQuery .text().

jQuery
var lines = [];
$("li").each(function (index, element) {
  lines.push(
    index + " id=" + this.id +
    " text=" + $(element).text()
  );
});
$("#out").text(lines.join("\n"));
Try It Yourself

How It Works

  1. this. Raw DOM node — use this.id.
  2. element. Same node as the second callback argument.
  3. $(element). jQuery wrapper for .text().

Example 3 — Prefer Implicit Iteration

Official tip: do not wrap a simple shared action in .each().

jQuery
// Unnecessary each
$("li").each(function () {
  $(this).text(function (i, t) { return t + " *"; });
});

// Clearer — same idea without each
// $("li").append(" *");

$("#out").text("Marked " + $("li").length + " items.");
Try It Yourself

How It Works

  1. Each. The loop applies the same edit to every item.
  2. Implicit. Methods like .append() already loop the set.
  3. When needed. Keep .each() for index-based or branching logic.

Frequently Asked Questions

.each() iterates over every DOM element in the current jQuery collection and runs a callback once per element. The callback receives the zero-based index and the DOM element; inside the callback, this refers to the current element.
this is the raw DOM element. $(this) wraps it in a jQuery object so you can use .text(), .addClass(), and other jQuery methods. Use this for native properties; use $(this) for jQuery APIs.
Return false from the callback to break out of the loop immediately — similar to break in a for loop. Returning anything else (or nothing) continues to the next element.
Many jQuery methods already loop the collection — $("li").addClass("bar") needs no .each(). Use .each() when each element needs different logic, when you need the index, or when native DOM APIs on this are simpler.
No. $("li").each(fn) loops matched DOM elements. jQuery.each(arrayOrObject, fn) is a static utility for plain arrays or objects. This page covers the instance method.
No. .each() returns the original jQuery object, so you can chain more methods after the loop. The callback may change elements, but the collection is the same set.

Next: .map() Method — Transform Matched Elements

Return to the previous jQuery set after a chain step.

.map() Method — Transform Matched Elements tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful