.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)
Pick a key, set a value, then read it back when you need it.
Select the element that should own the data.
Set with .data(key, value) or .data({ key: value }).
Read with .data(key) or .data() for the whole object.
Clear with .removeData(key) when finished.
Piece
Required?
Role
key
Yes*
String name (*omit for full object getter)
value
Optional
Any JS type except undefined
obj
Optional
Shallow-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
Reference
Cheat Sheet
Keep these facts handy while attaching element data.
Topic
Detail
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
undefined
Not stored — setter returns jQuery only
Avoid on
object, applet, embed
Try it
Live Preview
Set and read keys on a demo element with .data().
Browser demo of .data() — same idea as the examples below.
Remove..removeData("blah") deletes that cache key.
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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.data()Universal
Bottom line: Safe for everyday DOM apps. Pair with .removeData() to avoid holding large objects longer than needed.
Analysis
Performance Notes
Approach
Cost
Best for
.data(key, value)
Cheap
Per-element state
Huge objects in .data()
Memory heavy
Avoid long-lived large payloads
.attr("data-*") every time
String only
When markup/CSS must see the value
Prefer .data() for runtime state. Clear unused keys with .removeData(), especially on long-lived SPA pages.
Remember
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.