jQuery.data()
methodThe jQuery.data()
method in jQuery is used to store any data associated with the specified element and/or return the value that was set.
jQuery.data()
method SyntaxjQuery.data(element,key,value) jQuery.data(element,key) jQuery.data(element)
Parameter | Type | Description |
---|---|---|
element |
Element | DOM element |
key |
String | data key |
value |
Anything | data value |
jQuery.data(element,key,value)
Store arbitrary data associated with the specified element. Returns the value that was set.
Store and retrieve a value from the div element
var div = $('div:eq(0)'); jQuery.data(div,"data",{duration:10, state: 'play'}); var data = jQuery.data(div,"data"); console.log(data.duration); console.log(data.state);
jQuery.data(element,key)
Return data specified by key
and store for the element
.
Get the data named "foo" stored at for an element.
var div = $('div:eq(0)'); var foo = jQuery.data(div,'foo'); console.log(foo);
jQuery.data( element )
Return value at the full data store for the element.
Get all the data for first div.
var div = $('div:eq(0)'); var data = jQuery.data(div); for (var key in data) { if (data.hasOwnProperty(key)) { console.log(key + " -> " + data[key]); } }