jQuery .map() Method

Beginner
⏱️ 12 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Transform set

What You’ll Learn

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

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.

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.

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

Official jQuery API checkbox example

jQuery
$( ":checkbox" )
  .map( function () {
    return this.id;
  } )
  .get()
  .join();
// "two,four,six,eight"

⚡ Quick Reference

GoalCode
Comma-separated checkbox IDs$(":checkbox").map(function(){ return this.id; }).get().join()
All input values as array$("input").map(function(){ return $(this).val(); }).get()
Skip an element (filter)return null; or return undefined;
Insert multiple per elementreturn [a, b];
Side effects only (no collect).each(function(){ ... })
Plain array utilityjQuery.map([1,2,3], fn)

📋 .map() vs .each() vs jQuery.map()

Three ways to loop — transform collection, side effects, or plain array utility.

.map()
new jQ

Collect callback return values

.each()
same jQ

Side effects; original set unchanged

jQuery.map()
array

Static utility for plain arrays/objects

.get()
unwrap

Plain array after .map()

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

Official jQuery demos for collecting data from matched elements.

Example 1 — Official Demo: Comma-Separated Checkbox IDs

Map each checkbox to its id, unwrap with .get(), and join — the core official pattern.

jQuery
$( ":checkbox" )
  .map( function () {
    return this.id;
  } )
  .get()
  .join();
Try It Yourself

How It Works

Each callback returns a string ID. .get() produces a plain array; native .join() (with default comma) builds the final string.

Example 2 — Official Demo: List All Form Input Values

Build a comma-separated list of every input value — official Example 1 pattern.

jQuery
$( "p" ).append(
  $( "input" ).map( function () {
    return $( this ).val();
  } )
  .get()
  .join( ", " )
);
Try It Yourself

How It Works

.map() extracts each value; the result jQuery object is unwrapped and joined before appending to the paragraph.

📈 Practical Patterns

Transform list items, equalize heights, and compare with .each().

Example 3 — Official Demo: Transform, Skip, and Expand List Items

Return DOM nodes, null to skip, or arrays to duplicate — official Example 2 pattern (simplified).

jQuery
var mappedItems = $( "li" ).map( function ( index ) {
  var replacement = $( " " ).text( $( this ).text() ).get( 0 );
  if ( index === 0 ) {
    $( replacement ).text( $( replacement ).text().toUpperCase() );
  } else if ( index === 1 || index === 3 ) {
    replacement = null;  // skip 2nd and 4th
  } else if ( index === 2 ) {
    replacement = [ replacement, $( " " ).text( "Extra" ).get( 0 ) ];
  }
  return replacement;
});
$( "#results" ).append( mappedItems );
Try It Yourself

How It Works

Returning null omits an item. Returning an array inserts multiple DOM nodes. The result is a new jQuery collection of transformed elements.

Example 4 — Official Demo: Equalize Heights with .map()

Map divs to their pixel heights, find the max, apply to all — official plugin pattern from Example 3.

jQuery
$.fn.equalizeHeights = function () {
  var maxHeight = this.map( function ( i, e ) {
    return $( e ).height();
  } ).get();
  return this.height( Math.max.apply( null, maxHeight ) );
};
$( "div" ).equalizeHeights();
Try It Yourself

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
Try It Yourself

How It Works

Use .map() when you need extracted or transformed data. Use .each() when you modify elements in place and do not need a collected result.

🚀 Common Use Cases

  • Form serialization helpers — collect input values or names with .map().
  • Checked item IDs$(":checked").map(function(){ return this.value; }).get().
  • 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().

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

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 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
.map() Universal

Bottom line: Safe in any jQuery project. Use .map() to transform collections; use .each() for in-place side effects.

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.

💡 Best Practices

✅ Do

  • Use .map() when you need collected return values
  • 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()

Key Takeaways

Knowledge Unlocked

Five things to remember about .map()

Collect callback returns.

5
Core concepts
.get 02

Unwrap

Array

Chain
03

null

Skip

Return
[] 04

Array

Expand

Return
loop 05

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

Back to jQuery Traversing

Explore more DOM traversal methods as the collection grows.

Traversing hub →

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.

6 people found this page helpful