jQuery.data()Method
Example
Store data on a div element first, then retrieve it
Try it Yourself »
Definition and Usage
$.data() function is used to store and retrieve data on the specified element, and returns the set value.
Tip:1. This is a low-level method; the .data() method is more convenient to use.
2. Data stored and retrieved via the data() function is temporary; once the page is refreshed, all previously stored data will be removed.
3. This method currently does not provide cross-platform setting on XML documents. Internet Explorer does not allow attaching data to XML documents via custom attributes.
Syntax
Usage 1
Note:1. Data stored and retrieved via the data() function is temporary; once the page is refreshed, all previously stored data will be removed.
2. undefined is an unrecognized data value. Calling jQuery.data( el, "name", undefined ) will return the corresponding "name" data, equivalent to jQuery.data(el, "name" ).
We can set different values on an element and retrieve these values:
jQuery.data(document.body, 'foo', 52); jQuery.data(document.body, 'bar', 'test');
Usage 2
We can set different values on an element and retrieve these values:
alert(jQuery.data( document.body, 'foo' )); alert(jQuery.data( document.body ));
| Parameters | Description |
|---|---|
| element | Element type - The DOM object on which data is to be stored. |
| key | Optional. String type - The specified key name string. |
| value | Optional. Object type - Data of any type to be stored. |
More Examples
Retrieve Data
Retrieve the data named "blah" stored on the element.
Other Extensions
jQuery Miscellaneous Methods