jQuery .data() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.data() stores or reads arbitrary values on matched elements in jQuery’s internal cache (since 1.2.3) — safer than inventing expando properties yourself.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$("#el").data("foo", 52)           // set
$("#el").data("foo")               // → 52
$("#el").data({ bar: true })       // set many (1.4.3+)
$("#el").data()                    // → { foo: 52, bar: true }

// HTML5 data-* seeds the cache on first read:
// <div data-role="page" data-last-value="43">
$("#el").data("role")              // → "page"
$("#el").data("lastValue")         // → 43  (camelCase)
CallWhat it does
.data()Learn jQuery .data(): store and read values on elements, HTML5 data-* attributes, camelCase keys, .data vs .attr, removeData, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element that should own the data.
Step 2Set with .data(key, value) or .data({ key: value }).
Step 3Read with .data(key) or .data() for the whole object.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

Example 1 — Set and Get a Value

Store a number, then read it back.

jQuery
$("#box").data("foo", 52);
console.log($("#box").data("foo"));  // 52
Try It Yourself

How It Works

  1. Select. $("#box") is the element that owns the data.
  2. Set. .data("foo", 52) stores 52 under the key foo.
  3. Get. .data("foo") returns that value from the cache.

Example 2 — Store an Object

Attach a structured value and read its properties.

jQuery
$("div").data("test", { first: 16, last: "pizza!" });
console.log($("div").data("test").first);  // 16
console.log($("div").data("test").last);   // "pizza!"
Try It Yourself

How It Works

  1. Store. The whole object is saved under the key test.
  2. Read. .data("test") returns that object reference.
  3. Dot access. Use .first / .last like any JS object.

Example 3 — Read HTML5 data-* Attributes

On first access, jQuery seeds the cache from markup.

jQuery
// <div id="page" data-role="page" data-last-value="43" data-hidden="true">
console.log($("#page").data("role"));       // "page"
console.log($("#page").data("lastValue"));  // 43
console.log($("#page").data("hidden"));     // true
Try It Yourself

How It Works

  1. Markup. data-* attributes declare initial values in HTML.
  2. First read. jQuery copies them into the cache and converts types.
  3. CamelCase. data-last-value becomes lastValue.

Frequently Asked Questions

.data() stores or reads arbitrary JavaScript values on matched elements in jQuery’s internal cache. You can set a key/value, set many keys from an object, read one key, or read the whole object. Available since jQuery 1.2.3.
data-* attributes live in markup. The first .data() call reads them into the cache (with type conversion). After that, .data() uses the cache — changing .data() does not update the attribute. Use .attr("data-key", value) to change the attribute itself.
$("#el").data(key, value) is the collection method. jQuery.data(element, key, value) is the static utility on a raw DOM node. Both use the same cache; prefer .data() in everyday code.
Since jQuery 1.4, .data() with no arguments returns a plain object of all stored keys for the first matched element. A missing key returns undefined. An element with no data yields {}.
No. .data("name", undefined) does not store undefined — it returns the jQuery object for chaining. Use null or omit the key instead.
Since jQuery 3, a dash plus a lowercase letter becomes camelCase (HTML dataset rules). data-last-value maps to .data("lastValue").
Do not use .data() on object, applet, or embed elements (except some Flash object cases). Browsers and plugins make those unreliable.

Next: removeData() Method — Clear Cached Element Data

Learn how to clear keys from the jQuery element data store.

removeData() Method — Clear Cached Element Data tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful