.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");
Call
What 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
Parameter
Description
Step 1
Select the elements — e.g. $("li").
Step 2
Call .each(function (index, element) { … }).
Step 3
Use this or element for the current DOM node.
Return Value
Return Value
Description
jQuery object
Most 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).
Each. The loop applies the same edit to every item.
Implicit. Methods like .append() already loop the set.
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.