Easy Tutorial
❮ Sel Input File Misc Callbacks Fired ❯

jQuery.data() Method

jQuery Miscellaneous Methods

Example

Store and then retrieve data on a div element

<div>
    Stored value is
    <span></span>
    and
    <span></span>
</div>
<script>
$(function () { 
    var div = $( "div" )[ 0 ];
    jQuery.data( div, "test", {
        first: 16,
        last: "pizza!"
    });
    $( "span:first" ).text( jQuery.data( div, "test" ).first );
    $( "span:last" ).text( jQuery.data( div, "test" ).last );
})
</script>

Definition and Usage

The $.data() function is used to store and retrieve data on a specified element, returning the set value.

Tip: 1. This is a low-level method, the .data() method is more convenient to use.


Syntax

Usage One

Note: 1. Data stored and retrieved using the data() function is temporary and will be removed upon page refresh.

We can set different values on an element and retrieve them:

jQuery.data(document.body, 'foo', 52);
jQuery.data(document.body, 'bar', 'test');

Usage Two

We can set different values on an element and retrieve them:

alert(jQuery.data( document.body, 'foo' ));
alert(jQuery.data( document.body ));
Parameter Description
element Element type The DOM object to store data on
key Optional. String type The specified key name string
value Optional. Object type The data of any type to be stored

More Examples

Retrieve Data


jQuery Miscellaneous Methods

❮ Sel Input File Misc Callbacks Fired ❯