jQuery.data() (alias $.data) attaches arbitrary JavaScript values to a DOM element without memory-leak risk from circular references (since jQuery 1.2.3). It is the low-level store behind jQuery’s data API.
Remember
$.data(element, key, value) → set (returns value)
$.data(element, key) → get one key
$.data(element) → full data object
$.data(document.body, "foo", 52);
$.data(document.body, "foo"); // 52
function data(element, key, value):
if value is provided and value is not undefined:
store[element][key] = value
return value
if key is provided:
return store[element][key]
return store[element] // full object (may include jQuery internals)
// Prefer everyday: $(el).data(key, value)
// Use $.data when you already hold a raw element
Reference
Cheat Sheet
Keep these facts handy while storing element data.
Topic
Detail
Syntax
jQuery.data(element [, key [, value]])
Since
jQuery 1.2.3 (full object get: 1.4)
undefined
Not a settable value — acts like a get
data-*
Not read by this low-level method alone
XML docs
Not reliably supported across browsers
Cleanup
$.removeData / jQuery DOM removal
Try it
Live Preview
Set, read, or clear a key on a hidden demo element with $.data.
demo
Browser demo of $.data() / $.removeData() — same idea as the examples below.
Result
$.data(el, "foo", 52)
$.data(el, "foo") → 52
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
Element. Use a raw DOM node (unwrap with $("#box")[0] if needed).
Store object. The value under "test" is a plain object.
Retrieve.jQuery.data(div, "test") returns the same object reference.
Read fields. Use normal property access: .first, .last.
Example 3 — Read the Full Data Store
Omit the key to get every entry on the element.
jQuery
var el = document.getElementById("panel");
$.data(el, "foo", 52);
$.data(el, "bar", "test");
var store = $.data(el);
console.log(store.foo); // 52
console.log(store.bar); // "test"
$.data(el, "name", undefined) behaves like a get for "name", not a set. Use null or removeData instead.
xml
XML documents are unreliable
jQuery notes this method does not provide cross-platform support for setting data on XML documents.
Compatibility
Browser Support
jQuery.data() needs a browser DOM. It has been available since jQuery 1.2.3+ in all modern browsers — it is not supported in Node.js, Bun, or Deno without a DOM.
✓ jQuery 1.2.3+ · Browsers only
jQuery.data()
Works in Chrome, Firefox, Safari, Edge, and other browsers with jQuery loaded. No Node.js support — data is attached to real DOM elements.
100%Browser support
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()Browsers
Bottom line: Use $.data(el, key, value) in the browser with a DOM element. Prefer $(el).data() for everyday code and data-* attributes.
Analysis
Performance Notes
Approach
Cost
Best for
$.data(el, key, value)
O(1)
Plugin state on a known element
$(el).data(...)
O(1) + convenience
Everyday code and data-*
Huge objects per element
Memory
Avoid storing giant trees if unused
Lookups are cheap. The real cost is holding large objects longer than needed — remove data when widgets tear down.
Remember
Key Takeaways
Role: attach any value (except undefined) to a DOM element.
Low-level: prefer $(el).data() for day-to-day use.
Cleanup: use $.removeData when done.
Browser: DOM only — not for Node.js.
One line:$.data(el, key, value) stores plugin state on a raw element — unwrap jQuery objects, and use .data() when you need data-*.
Frequently Asked Questions
jQuery.data(element, key, value) stores arbitrary JavaScript data on a DOM element. jQuery.data(element, key) reads one key. jQuery.data(element) returns the full internal data object for that element.
jQuery.data() is the low-level static utility — it needs a native DOM element. The collection .data() method is easier for chaining and also reads HTML5 data-* attributes on first access. Prefer .data() in everyday code.
No. The static method does not retrieve data-* attributes unless the higher-level .data() method has already parsed them into the internal store.
Yes. Any JavaScript type except undefined can be stored — numbers, strings, booleans, arrays, and objects. The official demo stores { first: 16, last: "pizza!" } on a div.
Use jQuery.removeData(element, key) for one key, or jQuery.removeData(element) to clear the store. jQuery also cleans up data when elements are removed through jQuery DOM methods.
No. It attaches data to browser DOM elements. Use it in the browser with jQuery loaded.