X
HOME HTML CSS JAVASCRIPT SQL PHP JQUERY ANGULAR BOOTSTRAP XML ASP.NET MORE...
REFERENCES | EXAMPLES | FORUM | ABOUT

jQuery Misc data() Method

jQuery Misc Methods 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 jQuery Misc Methods


Your suggestion:

Close [X]

Thank You For Helping Us!

Your message has been sent to W3Schools.

Close [X]
Search w3schools.com:

WEB HOSTING

UK Reseller Hosting

WEB BUILDING

Download XML Editor FREE Website BUILDER Free HTML5 Templates

SHARE THIS PAGE

facebook