jQuery .map()
Learn all about the jQuery function .map().
If you wish to process a plain array or object, use the jQuery.map() instead.
As the return value is a jQuery object, which contains an array, it’s very common to call .get()
on the result to work with a basic array.
The .map()
method is particularly useful for getting or setting the value of a collection of elements. Consider a form with a set of checkboxes in it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
|
To get a comma-separated list of checkbox ID
s:
1
2
3
4
5
6
|
|
The result of this call is the string, "two,four,six,eight"
.
Within the callback function, this
refers to the current DOM element for each iteration. The function can return an individual data item or an array of data items to be inserted into the resulting set. If an array is returned, the elements inside the array are inserted into the set. If the function returns null
or undefined
, no element will be inserted.