jQuery Miscellaneous data() method
Example
towards
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 »
Definition and Usage
The data() method attaches data to selected elements, or retrieves data from selected elements.
**Tip:** To remove data, use removeData() method.
Return data from an element
Return the attached data from the selected element.
Syntax
$(
selector
).data(
name
)
| Parameters | Description |
|---|---|
| name | Optional. Specifies the name of the data to retrieve. |
| If no name is specified, the method returns all stored data from the element as an object. |
Attach data to an element
Attach data to the selected element.
Syntax
$(
selector
).data(
na
me,value
)
| Parameters | 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 the selected element using an object with name/value pairs.
Syntax
$(
selector
).data(
object
)
Try it »
| Parameters | Description |
|---|---|
| object | Required. Specifies an object containing name/value pairs. |
jQuery Miscellaneous Methods