.map( callback )Returns: jQuery
Description: Pass each element in the current matched set through a function, producing a new jQuery object containing the return values.
-
version added: 1.2.map( callback )
-
callbackA function object that will be invoked for each element in the current set.
-
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.
Examples:
Build a list of all the values within a form.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
|
Demo:
A contrived example to show some functionality.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
|
Demo:
Equalize the heights of the divs.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
|
|