jQuery .map() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Traversing

Definition

.map() (since jQuery 1.2) runs a callback on every matched element and builds a new jQuery object from the values you return. Ideal for collecting IDs, values, text, or numbers from a set.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
// Collect checkbox IDs → plain array → joined string
$(":checkbox")
  .map(function () {
    return this.id;
  })
  .get()
  .join();

// Skip an item: return null or undefined
// Expand: return an array of items
CallWhat it does
.map(callback)Build a new jQuery object from return values
callback(index, element)this = current DOM element
Return valueInserted into the result set
Return arrayEach item inserted (expand)
Return null / undefinedSkip — nothing inserted

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
callbackUsed in .map(callback). Build a new jQuery object from return values
indexUsed in callback(index, element). this = current DOM element
elementUsed in callback(index, element). this = current DOM element

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Collect Checkbox IDs

Official pattern: map ids, then .get().join().

jQuery
var ids = $(":checkbox").map(function () {
  return this.id;
}).get().join();

$("#out").text("IDs: " + ids);
// → "IDs: two,four,six,eight"
Try It Yourself

How It Works

  1. Select. $(":checkbox") finds every checkbox.
  2. Map. Return this.id for each one.
  3. Unwrap. .get() gives a plain array.

Example 2 — Collect Form Input Values

Official Ex 1 idea: map .val() from every input, then join.

jQuery
$("p").append(
  $("input").map(function () {
    return $(this).val();
  }).get().join(", ")
);
// Values: 2, 4, 6, 8
Try It Yourself

How It Works

  1. Select. All input fields in the form.
  2. Read. $(this).val() returns each field value.
  3. Collect. .map() builds a jQuery-wrapped list of strings.

Example 3 — Skip with null and Expand with an Array

Return null to drop an item; return an array to insert more than one.

jQuery
var items = $("li").map(function (index) {
  var text = $(this).text();
  if (index === 1 || index === 3) {
    return null; // skip Second, Fourth
  }
  if (index === 2) {
    return [text, text + " Extra"]; // expand Third
  }
  return text;
}).get();

$("#out").text(items.join(" | "));
// First | Third | Third Extra | Fifth
Try It Yourself

How It Works

  1. Skip. null/undefined inserts nothing.
  2. Expand. An array inserts each of its items.
  3. Keep. A single return value inserts one item.

Frequently Asked Questions

.map(callback) passes each element in the current matched set through a function and builds a new jQuery object from the return values. Available since jQuery 1.2.
.each() runs a callback for side effects and returns the original collection. .map() collects return values into a new jQuery object. Use .each() to change elements; use .map() when you need transformed results.
$('li').map(fn) loops matched DOM elements. jQuery.map(arrayOrObject, fn) is a static utility for plain arrays or objects. This page covers the instance method.
.map() returns a jQuery object wrapping results. .get() unwraps it to a plain JavaScript array — needed before .join(), Math.max.apply, or other native array APIs.
Nothing is inserted for that iteration — the item is skipped. Returning an array inserts each item inside it, so you can filter or expand inside .map().
this is the current DOM element, same as .each(). The callback also receives (index, domElement).

Next: .eq() Method — Select by Index

Get the immediately following sibling of each matched element.

.eq() Method — Select by Index tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

18 people found this page helpful