The .map() traversing method transforms each element in the current set through a callback and builds a new jQuery object from the return values. This tutorial covers the official checkbox ID and form value demos, return-value rules (including null and arrays), the .get().join() pattern, and compares .map() with .each().
01
Callback
.map(fn)
02
Returns
New jQ obj
03
.get()
Plain array
04
null skip
Omit item
05
vs .each()
Transform
06
Since 1.2
Core API
Fundamentals
Introduction
Sometimes you need to extract data from a jQuery collection — checkbox IDs, input values, element heights — rather than modify the elements in place. The .map() method runs a callback on each matched element and collects whatever the callback returns into a new jQuery object.
Available since jQuery 1.2, .map(callback) is the collection counterpart to side-effect iteration with .each(). It is particularly useful for building lists, comma-separated strings, or transformed DOM fragments from an existing set. Chain .get() when you need a plain JavaScript array.
Concept
Understanding the .map() Method
Given a jQuery object with matched DOM elements, .map(function(index, element) { return value; }) invokes the function once per element. Each return value is added to the result — a single item, an array of items (flattened in), or nothing if the callback returns null or undefined.
Inside the callback, this refers to the current DOM element. The return type can be a string, number, DOM node, jQuery object, or array of any of these. The final result is always wrapped in a new jQuery object.
💡
Beginner Tip
$(":checkbox").map(function(){ return this.id; }).get().join() produces "two,four,six,eight" — the official pattern for turning a collection into a comma-separated string.
Foundation
📝 Syntax
General form of .map:
jQuery
.map( callback )
Parameters
callback (required) — a function invoked for each element; receives index and DOM element; this is the current element. Return a value to include in the result, null/undefined to skip, or an array to insert multiple items.
Return value
A new jQuery object containing the collected return values from the callback.
All divs set to tallest height
.map() collects heights → Math.max → .height()
How It Works
.map() gathers numeric heights into an array via .get(). Math.max.apply finds the largest; all divs get that height.
Example 5 — .map() vs .each() on the Same List
See how collecting return values differs from side-effect iteration.
jQuery
var texts = $( "#map-demo li" ).map( function () {
return $( this ).text();
} ).get().join( " | " );
$( "#map-demo li" ).each( function ( i ) {
$( this ).css( "color", i % 2 ? "blue" : "green" );
});
// .map() → string of text | .each() → colors each li
Build option lists — map elements to new DOM nodes for re-insertion.
Height/width arrays — plugin math like equalize-heights pattern.
Extract text or attributes — .map(function(){ return this.dataset.id; }).
Filter via null — skip elements by returning null instead of using .filter().
🧠 How .map() Collects Return Values
1
Start with collection
jQuery object holds matched DOM elements.
Input
2
Run callback each
Function returns value, array, or null.
Transform
3
Collect results
Non-null returns added to new set.
Build
4
🔄
Return & chain
New jQuery object — often followed by .get().
Important
📝 Notes
Available since jQuery 1.2 — callback required.
Returns a new jQuery object — not the original matched set.
Return null or undefined to skip an element in the result.
Return an array to insert multiple values for one iteration.
Chain .get() for a plain JavaScript array before .join() or Math.max.
For plain arrays/objects (not DOM collections), use static jQuery.map() instead.
Compatibility
Browser Support
.map() has been part of jQuery since 1.2+. It relies on callback iteration with no browser-specific behavior.
✓ jQuery 1.2+
jQuery .map()
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: Array.prototype.map on a converted NodeList — but jQuery .map() handles DOM nodes and mixed return types.
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
.map()Universal
Bottom line: Safe in any jQuery project. Use .map() to transform collections; use .each() for in-place side effects.
Wrap Up
Conclusion
The jQuery .map() method transforms each element in a matched collection through a callback and returns a new jQuery object built from the collected return values. It is the go-to tool for extracting IDs, values, dimensions, or rebuilt DOM fragments.
Remember the official checkbox lesson: .map(function(){ return this.id; }).get().join() produces a comma-separated ID string. Return null to skip, return arrays to expand, and use .each() when you only need side effects without collecting results.
Chain .get() before native array methods like .join()
Return null to omit elements instead of post-filtering
Use this inside the callback for the current DOM node
Prefer .each() when you only modify elements in place
❌ Don’t
Use .map() for side effects only — use .each()
Confuse $("li").map() with static jQuery.map()
Forget that the result is a new jQuery object, not a plain array
Assume returning nothing keeps the original element — it is skipped
Chain jQuery methods expecting the original matched set after .map()
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .map()
Collect callback returns.
5
Core concepts
🔄01
.map()
Transform
API
.get02
Unwrap
Array
Chain
∅03
null
Skip
Return
[]04
Array
Expand
Return
loop05
.each()
Side fx
Compare
❓ Frequently Asked Questions
.map(callback) passes each element in the current jQuery collection through a callback function and builds a new jQuery object from the values returned. It transforms the set — each callback return becomes part of the result.
.each() runs a callback for side effects and returns the original jQuery object unchanged. .map() collects return values from the callback into a new jQuery object. Use .each() to modify elements; use .map() when you need transformed results.
$('li').map(fn) is an instance method on a jQuery DOM collection — it loops matched elements. jQuery.map(arrayOrObject, fn) is a static utility for plain JavaScript arrays or objects. This tutorial covers the instance method.
.map() returns a jQuery object wrapping an array of results. .get() unwraps it to a plain JavaScript array — essential before .join(), Math.max.apply, or other native array methods.
Nothing is inserted for that iteration — the element is skipped in the result set. Returning an array inserts each item in that array. This lets you filter or expand results inside .map().
Inside the callback, this refers to the current DOM element for each iteration — the same as .each(). The callback also receives index and element parameters.
Did you know?
jQuery has two map APIs: the instance method $("li").map(fn) on DOM collections (since 1.2) and the static utility jQuery.map(array, fn) for plain JavaScript arrays. They share a name and similar callback shape, but only the instance method operates on matched DOM elements in a jQuery object.