.data( key, value )Returns: jQuery
Description: Store arbitrary data associated with the matched elements.
-
version added: 1.2.3.data( key, value )
-
version added: 1.4.3.data( obj )
-
objType: ObjectAn object of key-value pairs of data to update.
-
.data()
method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore from memory leaks.
1
2
3
4
5
|
|
data()
method to update data does not affect attributes in the DOM. To set a data-*
attribute value, use attr
.
.data( obj )
completely replaced all data. Since jQuery 1.4.3, data is instead extended by shallow merge.
$( "body" ).data( { "my-name": "aValue" } ).data();
will return { myName: "aValue" }
.
.data()
method cannot be used on <object>
(unless it's a Flash plugin), <applet>
or <embed>
elements.
Additional Notes:
- Note that this method currently does not provide cross-platform support for setting data on XML documents, as Internet Explorer does not allow data to be attached via expando properties.
-
undefined
is not recognized as a data value. Calls such as.data( "name", undefined )
will return the jQuery object that it was called on, allowing for chaining.
Example:
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:
.data( key )Returns: Object
Description: Return arbitrary data associated with the first element in the jQuery collection, as set by data() or by an HTML5 data-*
attribute.
-
version added: 1.2.3.data( key )
-
keyType: StringName of the data stored.
-
-
version added: 1.4.data()
- This signature does not accept any arguments.
.data()
method allows us to read data previously associated with DOM elements. We can retrieve several distinct values for a single element one at a time, or as a set:
1
2
3
4
5
6
7
|
|
.data()
with no parameters returns a JavaScript object containing each stored value as a property. The object can be used directly to get data values (but note that property names originally containing dashes will have been modified as described below).
$( "body" ).data( { "my-name": "aValue" } ).data();
will return { myName: "aValue" }
.
HTML5 data-*
Attributes
data-*
attributes< are used to initialize jQuery data. An element's data-*
attributes are retrieved the first time the data()
method is invoked upon it, and then are no longer accessed or mutated (all values are stored internally by jQuery).
jQuery.parseJSON
is used to parse it; it must follow valid JSON syntax< including quoted property names. A string not parseable as a JavaScript value is not converted.
data-*
attribute value as an unconverted string, use the attr()
method.
data-*
attribute names have been processed in alignment with the HTML dataset API<.
1
|
|
1
2
3
4
|
|
Additional Notes:
- Note that this method currently does not provide cross-platform support for setting data on XML documents, as Internet Explorer does not allow data to be attached via expando properties.
Example:
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
61
|
|