The jQuery.data() utility attaches arbitrary JavaScript values to DOM elements — safely and without memory leaks. This tutorial covers set/get syntax, official object storage demos, reading the full data store, comparisons with .data() (preferred collection method), and pairing with removeData.
01
Set
key + value
02
Get
one key
03
All
full store
04
DOM
element arg
05
Objects
any type
06
vs .data()
static
Fundamentals
Introduction
This page covers the static utilityjQuery.data() (same API as api.jquery.com/jQuery.data). The preferred collection method .data() is documented under DOM.
Sometimes you need to remember information about a DOM element — plugin state, cached calculations, widget options, or temporary flags — without cluttering the HTML with extra attributes or global variables.
jQuery provides jQuery.data() (also written $.data()) as a low-level static utility to attach any JavaScript value to an element. jQuery manages the storage internally, avoids circular-reference leaks, and cleans up when elements are removed through jQuery. For everyday chaining, the instance method .data() on jQuery collections is usually more convenient but understanding the static API matters for plugins and internals.
Concept
Understanding the jQuery.data() Method
jQuery.data(element, key, value) stores value under key on the given DOM element and returns the value that was set.
jQuery.data(element, key) retrieves a single stored value. jQuery.data(element) with no key returns the entire internal data object — including keys jQuery itself uses for events.
💡
Beginner Tip
Pass a native DOM element — $("#box")[0], not $("#box"). Also, undefined cannot be stored; passing it as a value behaves like a get call instead.
Foundation
📝 Syntax
Three overloads of jQuery.data:
jQuery
// Set a value (returns the value set)
jQuery.data( element, key, value )
// Get one key
jQuery.data( element, key )
// Get entire data store (since jQuery 1.4)
jQuery.data( element )
Parameters
element — the DOM element to associate with data.
key — string name for the stored value (omit when reading all data).
value — any JavaScript type except undefined.
Return value
When setting: returns the stored value.
When getting one key: returns the stored value or undefined if missing.
When getting all: returns an object of all keys (may include jQuery internals).
Minimal workflow
jQuery
var el = document.getElementById("widget");
$.data(el, "open", true);
if ($.data(el, "open")) {
// widget is open
}
Cheat Sheet
⚡ Quick Reference
Call
Action
$.data(el, "count", 5)
Store number 5
$.data(el, "count")
Read key count
$.data(el)
Read all keys
$.removeData(el, "count")
Delete one key
$(el).data("count", 5)
Instance method (chaining)
data-* attribute alone
Not read by static $.data
undefined as value
Not stored — acts like get
Compare
📋 $.data vs .data() vs data-* attributes
Three related patterns — static store, jQuery chaining, and HTML markup.
$.data
static DOM
Low-level utility
.data()
$(el).data
Reads data-* too
data-*
HTML attr
Markup config
removeData
delete key
Cleanup pair
Hands-On
Examples Gallery
Each example uses $.data() with jQuery 3.7.1. Open DevTools or use the Try-it links to run them interactively.
📚 Getting Started
Official jQuery API patterns — multiple keys on one element.
Example 1 — Set and Get Simple Values
From the jQuery docs — store a number and a string on document.body.
Objects are stored by reference in jQuery’s cache. Mutating the returned object updates the cached value — useful for widget state bags.
Example 3 — Read the Full Data Store
Call $.data(element) with no key to retrieve all stored values.
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
Plugins attach private state to their root element. jQuery’s cache keeps it off the DOM and releases it when the element is removed — a core reason $.data exists.
Applications
🚀 Common Use Cases
Plugin state — store widget options and flags on root elements.
Cached computations — remember expensive layout or size results.
Event metadata — jQuery itself stores handler data internally.
Drag-and-drop — track source index or payload on dragged nodes.
Lazy initialization — mark elements as already enhanced.
Avoid globals — keep per-element data off window.
🧠 How jQuery.data() Stores Values Safely
1
Resolve element
Accept a native DOM node — unwrap jQuery collections first.
Input
2
Internal cache
jQuery maps element IDs to a data object — no expando attrs on the node itself.
Store
3
Set or get key
Three-arg form writes; two-arg reads one key; one-arg reads all.
Access
4
✅
Auto cleanup
Data is removed when jQuery removes the element — reducing memory leaks.
Important
📝 Notes
Available since jQuery 1.2.3 — full-store read since 1.4.
Low-level API — prefer .data() for chaining and data-* support.
Static $.data does not read HTML data-* attributes by itself.
undefined cannot be stored; passing it as value triggers a get instead.
jQuery stores its own internal keys — inspect carefully when dumping all data.
Setting data on XML documents is not fully cross-browser — stick to HTML elements.
jQuery.data() has been part of jQuery since jQuery 1.2.3+. It works wherever jQuery runs — all modern browsers and legacy IE when paired with a supported jQuery build.
✓ jQuery 1.2.3+
jQuery.data()
Supported in jQuery 1.x, 2.x, and 3.x. Safe element-associated storage with automatic cleanup on jQuery DOM removal.
100%With jQuery loaded
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
jQuery.data()Universal
Bottom line: Safe in any project that already includes jQuery. For new app code, $(el).data() is often more ergonomic — but $.data remains the static utility plugins rely on.
Wrap Up
Conclusion
The jQuery.data() utility attaches arbitrary JavaScript values to DOM elements through jQuery’s internal cache — without memory-leaking expando properties on the nodes themselves.
Remember: unwrap to a DOM element, use three args to set and two to get, pair with removeData for cleanup, and reach for .data() when you need data-* attributes or jQuery chaining.
jQuery.data(element, key, value) stores arbitrary JavaScript data on a DOM element and returns the value set. jQuery.data(element, key) retrieves one key. jQuery.data(element) with no key returns the full internal data object for that element.
jQuery.data() is the low-level static utility — it requires a native DOM element. The .data() method on jQuery collections is more convenient for chaining and also reads HTML5 data-* attributes on first access. Prefer the collection .data() method in everyday code; use jQuery.data() when you already hold a raw element. The .data() tutorial lives under DOM.
No. The static jQuery.data() method does not retrieve data-* attributes unless the higher-level .data() method has already parsed them into the internal store. For attribute-backed values, use $(element).data() or read attributes directly.
Yes. Any JavaScript type except undefined can be stored — numbers, strings, booleans, arrays, and plain objects. The official jQuery demo stores { first: 16, last: "pizza!" } on a div element.
Use jQuery.removeData(element, key) to delete one key, or jQuery.removeData(element) to clear the element's data store. jQuery also cleans up data when elements are removed through jQuery DOM methods.
jQuery.data() is a static utility like jQuery.contains(). Unwrap collections with [0] or .get(0). Passing a jQuery object will not read or write the data you expect on the underlying element.
Did you know?
Before jQuery’s data module, developers often stored values directly on DOM nodes (element.myValue = 42), which could cause memory leaks in older IE. jQuery’s separate cache — accessed through $.data — was designed specifically to attach data safely and release it when elements leave the document.