jQuery Tutorial Tutorials - jQuery jQuery.data() method

jQuery jQuery.data() method

The 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 jQuery.data() method Syntax

jQuery.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.

example

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);

Try now

jQuery.data(element,key)

Return data specified by key and store for the element .

example

Get the data named "foo" stored at for an element.

var div = $('div:eq(0)');
var foo = jQuery.data(div,'foo');
console.log(foo);

Try now

jQuery.data( element )

Return value at the full data store for the element.

example

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]);
    }
}

Try now

Date:2019-08-20 16:23:50 From:www.Lautturi.com author:Lautturi