.data() attaches arbitrary JavaScript values to elements and reads them back — including values seeded from HTML5 data-* attributes. This tutorial covers setters and getters, object merges, data-* conversion, camelCase keys, comparisons with .attr() and jQuery.data(), five examples, and try-it labs.
01
Set
Key / value
02
Get
By key
03
All keys
.data()
04
data-*
HTML5 seed
05
vs .attr()
Cache vs DOM
06
Since 1.2.3
Core API
Fundamentals
Introduction
This page covers the collection method .data() (same API as api.jquery.com/data). The low-level static jQuery.data() utility is documented under Utilities.
Plugins and UI widgets often need to remember state on a DOM node — options, counters, objects — without inventing fragile global variables. Since jQuery 1.2.3, .data() is the standard way to do that.
Values live in jQuery’s internal cache (not as expando properties you manage yourself), which helps avoid circular-reference memory leaks. You can store numbers, strings, objects, arrays, and more — anything except undefined.
Since 1.4.3, HTML5 data-* attributes also seed that cache the first time you call .data(). Pair this topic with .removeData(), jQuery.hasData(), and the DOM hub.
Concept
Understanding the .data() Method
Think of each element as having a small private dictionary. Writing $(el).data("foo", 52) puts 52 under key foo. Reading $(el).data("foo") returns it. Calling .data() with no arguments returns the whole dictionary for the first matched element.
Setting with an object (since 1.4.3) shallow-merges keys into the existing store — it no longer replaces the entire object like early 1.4 versions did.
💡
Beginner Tip
.data() does not change HTML attributes. Use .attr("data-foo", "bar") when you need the markup itself updated. Use .data() for runtime state and typed values.
Foundation
📝 Syntax
Common signatures:
jQuery
.data( key, value ) // set one key (1.2.3+)
.data( obj ) // merge keys from an object (1.4.3+)
.data( key ) // get one key (1.2.3+)
.data() // get all data as an object (1.4+)
Parameters
key (String) — name of the data entry.
value (Anything except undefined) — value to store.
obj (Object) — key/value pairs to merge into the cache.
Return value
Setter — the jQuery collection (chainable).
Getter with key — the stored value (or undefined).
Getter with no args — a plain object of all keys for the first element.
.data write → cache only
.attr write → markup string
Pick one source of truth per key
How It Works
Attributes are initialized into the cache once. After that, treat .data() as the runtime source of truth, or clear with .removeData() if you need to re-seed from attributes.
Applications
🚀 Common Use Cases
Plugin / widget state — options, timers, and flags on the host element.
Counters and IDs — remember which row or tab is active.
Config from markup — read data-* into typed values on init.
.data() has been part of jQuery since 1.2.3+ (with later signature expansions). It works in every browser supported by your jQuery build — including modern evergreen browsers with jQuery 3.x. The cache is implemented by jQuery.
✓ jQuery 1.2.3+
.data()
Stable element data API across supported jQuery versions. Prefer .data() on collections over the static utility.
100%With jQuery 1.2.3+
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()1.2.3+
Bottom line: Use for runtime state and typed data-* reads; use .attr() when you need the attribute string.
Wrap Up
Conclusion
.data() is jQuery’s friendly API for attaching typed values to elements and reading HTML5 data-* into that same store. Keep attributes and cache separate in your mental model, and clean up with .removeData() when widgets tear down.
Read data-* through .data() when you want typed values
Call .removeData() on destroy / reset
Prefer camelCase keys that match jQuery 3 rules
Document which keys your plugin owns
❌ Don’t
Expect .data() writes to update HTML attributes
Store undefined as a value
Put data on object / embed / applet
Mix attr and data as competing sources for the same key
Forget that getters use only the first matched element
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .data()
Store and read element-associated values safely.
5
Core concepts
+01
Set / get
By key
API
{}02
Merge
Object
1.4.3+
*03
data-*
Seeds
HTML5
≠04
Not
.attr()
Cache
⌫05
Clear
.removeData()
Cleanup
❓ Frequently Asked Questions
.data() stores or reads arbitrary JavaScript values associated with matched elements in jQuery’s internal cache (safe from common memory-leak patterns). You can set a key/value, set many keys from an object, read one key, or read the whole data object. Available since jQuery 1.2.3 (object setter and no-arg getter later).
data-* attributes live in the DOM markup. The first time you call .data() on an element, jQuery reads those attributes into its cache (with type conversion). After that, .data() uses the cache — changing .data() does not update the attribute. To change the attribute itself, use .attr("data-key", value).
.data() is the collection method: $("#el").data("key", value). jQuery.data(element, key, value) is the static utility that takes a raw DOM node. Both talk to the same cache; prefer .data() in everyday code. See the Utilities jQuery.data() tutorial for the static form.
Since jQuery 1.4, .data() with no arguments returns a plain object of all stored keys for the first matched element. Missing keys when reading a name return undefined. An element with no data yet yields {}.
No. undefined is not treated as a data value. Calling .data("name", undefined) does not store undefined — it returns the jQuery object for chaining (setter signature behavior). Use null or omit the key instead.
Since jQuery 3 (aligned with the HTML dataset API), a dash followed by a lowercase letter becomes camelCase. So data-last-value maps to .data("lastValue"). Writing .data({ "my-name": "aValue" }) and then .data() returns { myName: "aValue" }.
Did you know?
jQuery tries hard when converting data-* strings: "100" becomes the number 100, but strings like "1E02" or "100.000" stay strings because converting them would change how they look when turned back into text.