jQuery .data() Method

Beginner
⏱️ 10 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
DOM data

What You’ll Learn

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

Introduction

This page covers the static utility jQuery.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.

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.

📝 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

}

⚡ Quick Reference

CallAction
$.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 aloneNot read by static $.data
undefined as valueNot stored — acts like get

📋 $.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

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.

jQuery
jQuery.data(document.body, "foo", 52);

jQuery.data(document.body, "bar", "test");



console.log(jQuery.data(document.body, "foo")); // 52

console.log(jQuery.data(document.body, "bar")); // test
Try It Yourself

How It Works

Each call adds another key to the element’s internal cache. Values persist until removed or until the element is deleted through jQuery.

📈 Practical Patterns

Objects, full store reads, cleanup, and widget state.

Example 2 — Store an Object (Official Demo)

The canonical jQuery demo — attach a plain object and read its properties.

jQuery
var div = $("div")[0];



jQuery.data(div, "test", {

  first: 16,

  last: "pizza!"

});



console.log(jQuery.data(div, "test").first); // 16

console.log(jQuery.data(div, "test").last);  // pizza!
Try It Yourself

How It Works

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
Try It Yourself

How It Works

Since jQuery 1.4, omitting the key returns the whole cache object. Note: jQuery may also store internal keys — do not assume every property is yours.

Example 4 — Remove Data With jQuery.removeData()

Official interactive demo pattern — set, read, and delete the blah key.

jQuery
var div = $("div")[0];



jQuery.data(div, "blah", "hello");

console.log(jQuery.data(div, "blah")); // hello



jQuery.data(div, "blah", 86);

console.log(jQuery.data(div, "blah")); // 86



jQuery.removeData(div, "blah");

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

How It Works

Overwriting a key replaces the old value. removeData deletes the key entirely — the official demo uses buttons to cycle through these operations.

Example 5 — Plugin State on an Element

Store widget options and open state without polluting the DOM.

jQuery
function initTabs(el) {

  $.data(el, "tabs", { active: 0, count: 3 });

}



function setActiveTab(el, index) {

  var state = $.data(el, "tabs");

  state.active = index;

  console.log("active tab: " + state.active);

}



var root = document.getElementById("tabs");

initTabs(root);

setActiveTab(root, 2); // active tab: 2
Try It Yourself

How It Works

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.

🚀 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.

📝 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.
  • Related: jQuery.removeData(), .data(), .removeData().

Browser Support

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 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
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.

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.

💡 Best Practices

✅ Do

  • Pass native elements: $.data($el[0], key, val)
  • Use namespaced keys in plugins: myPlugin.options
  • Call removeData when discarding large objects
  • Prefer .data() for markup data-* config
  • Store objects for structured widget state

❌ Don’t

  • Pass jQuery objects directly to $.data
  • Expect static $.data to read data-* attrs
  • Store undefined as a meaningful value
  • Assume $.data(el) contains only your keys
  • Rely on data storage for XML nodes in legacy IE

Key Takeaways

Knowledge Unlocked

Five things to remember about jQuery.data()

Store per-element data the jQuery way.

5
Core concepts
📝 02

Set

3 args

Write
🔍 03

Get

2 args

Read
📦 04

Objects

Any type

Values
🗑 05

removeData

Cleanup

Delete

❓ Frequently Asked Questions

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.

Next: jQuery.removeData() Method

Parse HTML strings into detached DOM node arrays before inserting them into the page.

jQuery.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.

6 people found this page helpful