jQuery Miscdata()Method
Example
Attach data to a <div> element, and then retrieve the data:
$("#btn1").click(function(){
$("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
alert($("div").data("greeting"));
});
$("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.
Tip:To remove data, use theremoveData()method.
Return data from an element
Returns the attached data from selected elements.
Syntax
$(selector).data(name)
| Parameter | Description |
|---|---|
| name | Optional. Specifies the name of the data to retrieve. If no name is specified, the method will return all stored data from 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 the data to set. |
| value | Required. Specifies the value of the data to set. |
Attach data to an element using an object
Attach data to selected elements using an object containing name/value pairs.
Syntax
| Parameter | Description |
|---|---|
| object | Required. Specifies an object containing name/value pairs. |
Other Extensions
jQuery Misc Methods