Easy Tutorial
❮ Html Insertbefore Jquery Ref Selectors ❯

jQuery Miscellaneous data() Method

jQuery Miscellaneous Methods

Example

Attach data to a <div> element, then retrieve that data:

$("#btn1").click(function(){
    $("div").data("greeting", "Hello World");
});
$("#btn2").click(function(){
    alert($("div").data("greeting"));
});

Definition and Usage

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

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


Return Data from Elements

Returns the attached data from the selected elements.

Syntax

Parameter Description
name Optional. Specifies the name of the data to be retrieved. <br>If no name is specified, this method will return all stored data from the element in an object.

Attach Data to Elements

Attaches data to the selected elements.

Syntax

Parameter Description
name Required. Specifies the name of the data to be set.
value Required. Specifies the value of the data to be set.

Attach Data to Elements Using an Object

Attaches data to the selected elements using an object containing name/value pairs.

Syntax

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

jQuery Miscellaneous Methods

❮ Html Insertbefore Jquery Ref Selectors ❯