Store arbitrary data associated with the specified element and/or return the value that was set.
jQuery.data( element, key, value )Returns: ObjectDescription: Store arbitrary data associated with the specified element. Returns the value that was set.
element
The DOM element to associate with the data.
key
A string naming the piece of data to set.
value
The new data value; this can be any Javascript type except undefined
.
Note: This is a low-level method; a more convenient .data()
is also available.
The jQuery.data()
method allows us to attach data of any type to DOM elements in a way that is safe from circular references and therefore free from memory leaks. jQuery ensures that the data is removed when DOM elements are removed via jQuery methods, and when the user leaves the page. We can set several distinct values for a single element and retrieve them later:
1
2
Additional Notes:
jQuery.data( document.body, "foo", 52 );
jQuery.data( document.body, "bar", "test" );
undefined
is not recognized as a data value. Calls such as jQuery.data( el, "name", undefined )
will return the corresponding data for "name", and is therefore the same as jQuery.data( el, "name" )
.Store then retrieve a value from the div element.
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
Demo: jQuery.data( element, key )Returns: Object
<title>jQuery.data demo</title>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
var div = $( "div" )[ 0 ];
jQuery.data( div, "test", {
$( "span" ).first().text( jQuery.data( div, "test" ).first );
$( "span" ).last().text( jQuery.data( div, "test" ).last );
Description: Returns value at named data store for the element, as set by jQuery.data(element, name, value)
, or the full data store for the element.
element
The DOM element to query for the data.
key
Name of the data stored.
element
The DOM element to query for the data.
Note: This is a low-level method; a more convenient .data()
is also available.
Regarding HTML5 data-* attributes: This low-level method does NOT retrieve the data-*
attributes unless the more convenient .data()
method has already retrieved them.
The jQuery.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. We can retrieve several distinct values for a single element one at a time, or as a set:
1
2
alert( jQuery.data( document.body, "foo" ) );
alert( jQuery.data( document.body ) );
The above lines alert the data values that were set on the body
element. If nothing was set on that element, an empty string is returned.
Calling jQuery.data( element )
retrieves all of the element's associated values as a JavaScript object. Note that jQuery itself uses this method to store data for internal use, such as event handlers, so do not assume that it contains only data that your own code has stored.
Note: 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:Get the data named "blah" stored at for an element.
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:
<title>jQuery.data demo</title>
<script src="https://code.jquery.com/jquery-3.7.1.js"></script>
<button>Get "blah" from the div</button>
<button>Set "blah" to "hello"</button>
<button>Set "blah" to 86</button>
<button>Remove "blah" from the div</button>
<p>The "blah" value of this div is <span>?</span></p>
$( "button" ).on( "click", function() {
switch ( $( "button" ).index( this ) ) {
value = jQuery.data( div, "blah" );
jQuery.data( div, "blah", "hello" );
jQuery.data( div, "blah", 86 );
jQuery.removeData( div, "blah" );
$( "span" ).text( "" + value );
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4