Example
Attach data to an element, then retrieve the data:
$("#btn1").click(function(){
$("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
alert($("div").data("greeting"));
}); |
Try it yourself »
|
Definition and Usage
The data() method attaches data to, or gets data from, selected elements.
Return Data from an Element
Returns attached data from a selected element.
Syntax
Parameter |
Description |
name |
Optional. Specifies the name of data to retrieve.
If no name is specified, this method will return all stored data for the
element as an object |
Attach Data to an Element
Attach data to selected elements.
Syntax
$(selector).data(name,value) |
Parameter |
Description |
name |
Required. Specifies the name of data to set |
value |
Required. Specifies the value of data to set |
Attach Data to an Element Using an Object
Attach data to selected elements using an object with name/value pairs.
Syntax
Parameter |
Description |
object |
Required. Specifies an object containing name/value pairs |