The .each() traversing method runs a callback function for every element in the current jQuery collection. This tutorial covers index and this, wrapping with $(this), breaking early with return false, official jQuery click demos, and when explicit iteration beats implicit jQuery chaining.
01
Syntax
.each(fn)
02
Index
Starts at 0
03
this
DOM element
04
$(this)
jQuery wrap
05
Break
return false
06
Since 1.0
Core API
Fundamentals
Introduction
A jQuery selector often matches multiple DOM elements — every <li> in a list, every row in a table, every card in a grid. When you need to run custom logic on each matched element individually, jQuery provides .each().
Available since jQuery 1.0, .each() accepts a callback function that fires once per element. The callback receives the zero-based index and the DOM element; inside the function, this also points to that same element. The method returns the original jQuery object so you can keep chaining.
Concept
Understanding the .each() Method
Given a jQuery object with one or more matched elements, .each(function(index, element) { ... }) loops through the collection in document order. The callback’s this keyword refers to the current DOM node — use native properties like this.style directly, or wrap with $(this) for jQuery methods.
Return false from the callback to stop the loop immediately. Many jQuery methods perform implicit iteration on their own — $("li").addClass("active") touches every match without needing .each(). Reach for explicit .each() when each element needs different treatment or when you need the loop index.
💡
Beginner Tip
Inside the callback, element === this is always true. The second parameter is the same DOM node — use whichever reads clearer in your code.
Foundation
📝 Syntax
General form of .each:
jQuery
.each( function( index, element ) {
// this === element
} )
Parameters
function (required) — a callback executed once per matched element. Receives the zero-based index and the DOM element. this inside the callback refers to the current element.
Return value
The original jQuery object (unchanged collection), enabling further chaining after the loop.
Official jQuery API list iteration example
jQuery
$( "li" ).each( function ( index ) {
console.log( index + ": " + $( this ).text() );
} );
// 0: foo
// 1: bar
Cheat Sheet
⚡ Quick Reference
Goal
Code
Loop all matched elements
$("li").each(function(i, el) { ... })
Read text per element
$(this).text() inside callback
Native DOM property
this.style.color = "blue"
Stop loop early
return false; in callback
Same class on all (no .each needed)
$("li").addClass("active")
Loop plain array/object
jQuery.each(items, fn) (static utility)
Compare
📋 Explicit .each() vs Implicit Iteration
When to loop manually vs let jQuery handle every element automatically.
.each()
explicit
Custom per-element logic or index needed
.addClass()
implicit
Same operation on every match
return false
break
Stop .each() mid-loop
jQuery.each()
static
Arrays and plain objects, not DOM sets
jQuery
// Unnecessary — implicit iteration handles this:
$( "li" ).each( function () {
$( this ).addClass( "foo" );
} );
// Preferred when every element gets the same treatment:
$( "li" ).addClass( "bar" );
Hands-On
Examples Gallery
Examples 1–4 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Core iteration patterns from the jQuery API docs.
Example 1 — Log Index and Text for Each li
The introductory jQuery API pattern: iterate a list and print each item’s index and text content.
jQuery
$( "li" ).each( function ( index ) {
console.log( index + ": " + $( this ).text() );
} );
Click span → all li items toggle italic "example" class.
How It Works
$(this) inside the callback wraps the current DOM element so jQuery methods like .toggleClass() can be used. Here every item gets the same toggle — implicit iteration with $("li").toggleClass("example") would also work.
Example 4 — Official Demo: Break Early with return false
Color divs yellow one by one until the loop hits #stop, then exit — official Example 3.
jQuery
$( "button" ).on( "click", function () {
$( "div" ).each( function ( index, element ) {
$( element ).css( "backgroundColor", "yellow" );
if ( $( this ).is( "#stop" ) ) {
$( "span" ).text( "Stopped at div index #" + index );
return false;
}
} );
} );
Divs 1–3 turn yellow; loop stops at #stop.
Span shows "Stopped at div index #3".
How It Works
return false inside the callback breaks out of .each() immediately — like break in a for loop. Divs after #stop are never processed.
Example 5 — Per-Index Styling vs Implicit Iteration
When each element needs a different value based on its index, .each() is the right tool — implicit chaining cannot do this.
jQuery
$( ".bar" ).each( function ( index ) {
$( this ).css( "width", ( index + 1 ) * 40 + "px" );
} );
// Same width on all — no .each() needed:
// $( ".bar" ).css( "width", "120px" );
Bar 0 → 40px, bar 1 → 80px, bar 2 → 120px
Progressive widths require per-index logic.
How It Works
When the operation depends on index, explicit .each() is necessary. When every element gets the identical treatment, prefer direct chaining and let jQuery’s implicit iteration handle it.
Applications
🚀 Common Use Cases
Indexed styling — set width, opacity, or delay based on loop index.
Collect data — build an array of IDs or values by reading each element inside the callback.
Conditional processing — apply different logic per element and break early with return false.
Native DOM APIs — use this.dataset, this.value, or this.style directly.
Debug logging — inspect each matched node during development.
Batch validation — check every form field and stop at the first invalid input.
🧠 How .each() Iterates a Collection
1
Start with collection
jQuery object holds one or more matched DOM elements.
Input
2
Fire callback
Function runs with index, element, and this set to current node.
Original jQuery collection returned for further chaining.
Important
📝 Notes
Available since jQuery 1.0; works in all jQuery 1.x, 2.x, and 3.x versions.
Index parameter is zero-based — first element is index 0.
this and the element parameter refer to the same DOM node.
return false breaks the loop; return true or no return continues.
Do not wrap every jQuery call in .each() — prefer implicit iteration when possible.
For arrays/objects (not DOM sets), use the static jQuery.each() utility instead.
Compatibility
Browser Support
.each() has been part of jQuery since 1.0+. It is pure JavaScript iteration over a DOM collection and works wherever jQuery runs.
✓ jQuery 1.0+
jQuery .each()
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: forEach on NodeList (no return false break) or a for loop.
100%With jQuery loaded
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 in any jQuery project. Use .each() for per-element custom logic; skip it when implicit iteration suffices.
Wrap Up
Conclusion
The jQuery .each() method loops through every element in the current collection and runs your callback with the index, DOM element, and this context. It is essential when each node needs different treatment or when you need to break out early.
Remember the jQuery docs guidance: for uniform operations like adding a class, implicit iteration via chained methods is cleaner than wrapping in .each(). Save explicit loops for indexed logic, collection building, and conditional per-element work.
Use .each() when index-based or per-element logic is needed
Wrap with $(this) when calling jQuery methods inside the callback
Use return false to break early when a condition is met
Prefer implicit iteration for identical operations on all matches
Cache $(this) in a variable if used multiple times per iteration
❌ Don’t
Wrap .addClass() or .css() in unnecessary .each() loops
Confuse instance .each() with static jQuery.each()
Expect return false to work like that in .map() or .filter()
Modify the jQuery collection size while iterating (can cause surprises)
Forget that .each() returns the original object, not a new set
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .each()
Loop matched elements with a callback.
5
Core concepts
🖼01
.each()
Loop all
API
002
Index
From zero
Arg
●03
this
DOM node
Context
✖04
false
Break loop
Exit
⚡05
Implicit
Skip when same
Tip
❓ Frequently Asked Questions
.each() iterates over every DOM element in the current jQuery collection and runs a callback function 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 for the current iteration. $(this) wraps that element in a jQuery object so you can chain jQuery methods like .text(), .addClass(), or .css(). Use this for native properties (this.style, this.id); 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 any other value (or nothing) continues to the next element.
Many jQuery methods implicitly iterate the collection — $('li').addClass('bar') applies to every li without .each(). Use .each() when each element needs different logic, when you need the index, or when native DOM APIs on this are simpler than jQuery chaining.
No. $('li').each(fn) is an instance method that loops matched DOM elements. jQuery.each(arrayOrObject, fn) is a static utility for plain arrays or objects. This tutorial covers the instance method on jQuery collections.
No — .each() returns the original jQuery object unchanged, so you can chain further methods after the loop completes. The callback may modify elements, but the collection itself is the same set of references.
Did you know?
jQuery’s own documentation warns that .each() is often unnecessary: methods like .addClass(), .css(), and .hide() already loop through every element in the collection internally. The classic smell test — if your callback body is identical for every element, drop the .each() and chain the method directly.