jQuery .data() Method

Beginner
7 min read
Updated: Oct 2026
4 examples
Live preview

What Is .data()?

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

Remember
$("#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)
NeedUse
Store / read JS values$("#el").data(key[, value])
Change the data-* attribute$("#el").attr("data-key", value)
Clear a key$("#el").removeData(key)
Static form (DOM node)$.data(element, key, value)

How to Use .data()

Pick a key, set a value, then read it back when you need it.

  1. Select the element that should own the data.
  2. Set with .data(key, value) or .data({ key: value }).
  3. Read with .data(key) or .data() for the whole object.
  4. Clear with .removeData(key) when finished.
PieceRequired?Role
keyYes*String name (*omit for full object getter)
valueOptionalAny JS type except undefined
objOptionalShallow-merge many keys (1.4.3+)
Return—Setter → jQuery; getter → value or object

Pseudocode

Pseudocode
cache = jQuery private store for element

if first .data() call and element has data-* attrs:
    copy attrs into cache (with type conversion)
    // attrs are not read again after this

if setting key, value:
    if value is undefined: return jQuery (no store)
    cache[key] = value          // does NOT write attribute
    return jQuery

if setting object:
    shallow_merge(cache, object)
    return jQuery

if reading key:
    return cache[key]           // or undefined

if reading all:
    return cache object         // {} when empty

Cheat Sheet

Keep these facts handy while attaching element data.

TopicDetail
Set one.data(key, value) since 1.2.3
Set many.data(obj) since 1.4.3 (shallow merge)
Get one / all.data(key) / .data() (all since 1.4)
data-*Seeds cache on first read (1.4.3+)
Dashes (jQuery 3)data-last-value → lastValue
undefinedNot stored — setter returns jQuery only
Avoid onobject, applet, embed

Live Preview

Set and read keys on a demo element with .data().

Browser demo of .data() — same idea as the examples below.

Result
$("#el").data("foo", 52)
$("#el").data("foo") → 52

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

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.
  4. Chain. The setter returns the jQuery object for further chaining.

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.
  4. Note. Mutating the object mutates the cached value too.

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.
  4. Unconverted string. Use .attr("data-last-value") if you need the raw attribute text.

Example 4 — Clear a Key With .removeData()

Store a value, then remove it from the cache.

jQuery
$("div").data("blah", "hello");
console.log($("div").data("blah"));  // "hello"
$("div").removeData("blah");
console.log($("div").data("blah"));  // undefined
Try It Yourself

How It Works

  1. Set. blah is stored as "hello".
  2. Confirm. .data("blah") returns the string.
  3. Remove. .removeData("blah") deletes that cache key.
  4. After. Reading the key again yields undefined.

Edge Cases & Pitfalls

Check these before relying on .data() in production.

attr

Cache does not update data-*

Use .attr() when the attribute in the DOM must change for CSS or other scripts.

undefined

undefined is not stored

.data("x", undefined) only returns the jQuery object. Use null or .removeData().

plugins

Avoid object / embed / applet

Browsers and plugins make .data() unreliable on those elements.

clone

Clones need .clone(true) for data

Default .clone() copies markup only. Pass true to copy .data() and events.

Browser Support

.data() has been available since jQuery 1.2.3. Reading all keys arrived in 1.4; object setters and data-* seeding in 1.4.3; camelCase dash rules align with the dataset API in jQuery 3.

✓ Stable · jQuery 1.2.3+

jQuery .data()

Supported in all browsers that run a modern jQuery build. Prefer jQuery 3.x for dataset-aligned key names.

100% jQuery core method
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.data() Universal

Bottom line: Safe for everyday DOM apps. Pair with .removeData() to avoid holding large objects longer than needed.

Performance Notes

ApproachCostBest for
.data(key, value)CheapPer-element state
Huge objects in .data()Memory heavyAvoid long-lived large payloads
.attr("data-*") every timeString onlyWhen markup/CSS must see the value

Prefer .data() for runtime state. Clear unused keys with .removeData(), especially on long-lived SPA pages.

Key Takeaways

  • Role: attach JS values to elements via jQuery’s cache.
  • data-*: seeds the cache once; later use the cache.
  • vs attr: cache for JS; attributes for markup/CSS.
  • Cleanup: call .removeData() when keys are done.

One line: .data() is the jQuery way to hang typed state on a DOM element without polluting attributes.

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()

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

.removeData() tutorial →

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