Tutorials References Menu

jQuery Misc data() Method

❮ jQuery Misc Methods

Example

Attach data to a <div> 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.

Tip: To remove data, use the removeData() method.


Return Data from an Element

Returns attached data from a selected element.

Syntax

$(selector).data(name)

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

$(selector).data(object)
Try it Yourself »

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

❮ jQuery Misc Methods