.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");
Select a set, then run per-element logic in the callback.
Select the elements — e.g. $("li").
Call .each(function (index, element) { … }).
Use this or element for the current DOM node.
Wrap with $(this) when you need jQuery methods.
Return false to stop early; otherwise the loop continues.
Piece
Required?
Role
Matched set
Yes
Elements to iterate
function
Yes
Runs once per element
index
Optional param
Zero-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
Reference
Cheat Sheet
Keep these facts handy while looping a collection.
Topic
Detail
Syntax
.each(function)
Since
jQuery 1.0
Callback
function(index, element)
this
Current DOM element
Break
return false
vs $.each()
DOM set vs arrays/objects
Returns
Same jQuery object (not a new set)
Official demo
$("li").each(function (index) { … })
Try it
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.
Code
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).
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll 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.
Analysis
Performance Notes
Approach
Cost
Best for
Implicit $("li").addClass("x")
Cheapest
Same action on every match
$("li").each(fn)
Cheap
Per-item logic / index
Nested .each() on large trees
Moderate
Avoid 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.
Remember
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.