jQuery .each() Method

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is .each()?

.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.

Remember
// 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");
NeedUse
Loop matched elements$el.each(fn)
Same action on allImplicit methods (.addClass(), …)
Build a new array.map()
Loop arrays/objects$.each()
Test one condition.is()

How to Use .each()

Select a set, then run per-element logic in the callback.

  1. Select the elements — e.g. $("li").
  2. Call .each(function (index, element) { … }).
  3. Use this or element for the current DOM node.
  4. Wrap with $(this) when you need jQuery methods.
  5. Return false to stop early; otherwise the loop continues.
PieceRequired?Role
Matched setYesElements to iterate
functionYesRuns once per element
indexOptional paramZero-based loop counter
element / this—Current DOM element (same value)
Return value—Original jQuery set (chainable)

Pseudocode

Pseudocode
call $(set).each(callback)

for index from 0 to set.length - 1:
    element = set[index]
    result = callback.call(element, index, element)
    if result === false:
        stop looping   // like break

return the original jQuery set

Cheat Sheet

Keep these facts handy while looping a collection.

TopicDetail
Syntax.each(function)
SincejQuery 1.0
Callbackfunction(index, element)
thisCurrent DOM element
Breakreturn false
vs $.each()DOM set vs arrays/objects
ReturnsSame jQuery object (not a new set)
Official demo$("li").each(function (index) { … })

Live Preview

Number list items, stop early, or show when implicit iteration is enough.

Sample list below. .each() visits every li unless you return false.

  • foo
  • bar
  • baz
Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

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.
  4. Text. $(this).text() reads the current item.

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().
  4. Pick. Native props → this; jQuery APIs → $(this).

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.
  4. Clarity. Shorter chains are easier to read and maintain.

Example 4 — Stop Early with return false

Official idea: process items until a stop marker, then break.

jQuery
var done = [];
$("div.box").each(function (index) {
  done.push($(this).text());
  if ($(this).is("#stop")) {
    $("#out").text("Stopped at index #" + index + " → " + done.join(", "));
    return false;
  }
});
Try It Yourself

How It Works

  1. Visit. Each box is processed in document order.
  2. Test. .is("#stop") checks the stop marker.
  3. Break. return false ends the loop immediately.
  4. Skip. Later boxes are never visited.

Example 5 — Prefix Each Item with Its Index

Per-item logic that needs the index — a good use of .each().

jQuery
$("li").each(function (index) {
  $(this).text(function (i, t) {
    return (index + 1) + ". " + t;
  });
});
Try It Yourself

How It Works

  1. Index. Zero-based counter from .each().
  2. Display. Add 1 for human-friendly numbering.
  3. Update. Each item gets its own prefix.
  4. Why each. The new text depends on the item’s position.

Edge Cases & Pitfalls

Check these before adding an unnecessary .each().

implicit

Skip .each() for shared actions

$("li").addClass("x") already loops. Extra .each() only adds noise.

this

this is not a jQuery object

this.text() fails. Use $(this).text() or native this.textContent.

break

Only false stops the loop

return; or return true continues. Use return false to break.

utility

Not the same as $.each()

$("li").each() loops DOM matches. $.each(arr, fn) loops arrays/objects.

Browser Support

.each() is a core jQuery method since 1.0. It works in every browser that runs your loaded jQuery version.

✓ Stable · jQuery 1.0+

jQuery .each()

Universal across jQuery-supported browsers. Prefer implicit iteration when every element gets the same simple action.

1.0+ jQuery since
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.each() Universal

Bottom line: Safe for everyday use. Reach for .each() when you need the index, branching, or per-item native DOM access.

Performance Notes

ApproachCostBest for
Implicit $("li").addClass("x")CheapestSame action on every match
$("li").each(fn)CheapPer-item logic / index
Nested .each() on large treesModerateAvoid when a single query + method works

The loop itself is fine. Cost grows with set size and heavy work inside the callback — keep the body small and scope the selector first.

Key Takeaways

  • Role: run a callback once per matched element.
  • this: DOM element; wrap with $(this) for jQuery APIs.
  • Break: return false stops the loop early.
  • Prefer: implicit iteration when every element gets the same simple action.

One line: use .each() for per-item logic and indexes — not for wrapping a method that already loops the set.

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: .end() method

Return to the previous jQuery set after a chain step.

.end() tutorial →

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