.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
Call
What it does
.map(callback)
Build a new jQuery object from return values
callback(index, element)
this = current DOM element
Return value
Inserted into the result set
Return array
Each item inserted (expand)
Return null / undefined
Skip — nothing inserted
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
callback
Used in .map(callback). Build a new jQuery object from return values
index
Used in callback(index, element). this = current DOM element
element
Used in callback(index, element). this = current DOM element
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefined
Getters usually return a value from the first matched element, or undefined when nothing applies.
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
.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).