data() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:data


The data() method attaches data to, or gets data from, selected elements.

Syntax to get data

Parameter Require Description
name of data to retrieve.

Syntax to set data

Parameter Require Description
name of data to set
value Required.value of data to set

Attach Data to an Element Using an Object

Parameter Description
objectRequired. Specifies an object containing name/value pairs

If no name is specified, this method returns all stored data for the element as an object

The following code shows how to Attach data to a <div> element, then retrieve the data:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<script src=""></script>
        $("div").data("greeting", "Hello World");
    });/*  w ww  .j  a  va 2  s  . c o m*/

<button id="btn1">Attach data</button><br>
<button id="btn2">Get data attached</button>



Related Tutorials