jQuery.data() Method

Beginner
7 min read
Updated: Oct 2026
4 examples
Live preview

What Is jQuery.data()?

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
NeedUse
Raw element + key$.data(el, key, value)
Chaining / data-*$(el).data(key, value)
Delete a key$.removeData(el, key)

How to Use jQuery.data()

Pass a DOM element, a string key, and optionally a value.

  1. Get a native element: document.getElementById("box") or $("#box")[0].
  2. Set with $.data(el, "key", value) — any type except undefined.
  3. Read with $.data(el, "key"), or $.data(el) for the whole store.
  4. Remove with $.removeData(el, "key") when finished.
PieceRequired?Role
elementYesDOM element to attach data to
keyFor set/get one valueString name of the entry
valueFor setAny type except undefined

Pseudocode

Pseudocode
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

Cheat Sheet

Keep these facts handy while storing element data.

TopicDetail
SyntaxjQuery.data(element [, key [, value]])
SincejQuery 1.2.3 (full object get: 1.4)
undefinedNot a settable value — acts like a get
data-*Not read by this low-level method alone
XML docsNot reliably supported across browsers
Cleanup$.removeData / jQuery DOM removal

Live Preview

Set, read, or clear a key on a hidden demo element with $.data.

Browser demo of $.data() / $.removeData() — same idea as the examples below.

Result
$.data(el, "foo", 52)
$.data(el, "foo") → 52

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Set and Get Values

Official API pattern 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

  1. Set number. jQuery.data(body, "foo", 52) stores 52 under "foo".
  2. Set string. A second key "bar" holds "test" on the same element.
  3. Get. Passing only element + key returns that value.
  4. Independent keys. Many named values can live on one element.

Example 2 — Store an Object

Official demo: one key holds a whole object.

jQuery
var div = document.getElementById("box");

jQuery.data(div, "test", {
  first: 16,
  last: "pizza!"
});

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

How It Works

  1. Element. Use a raw DOM node (unwrap with $("#box")[0] if needed).
  2. Store object. The value under "test" is a plain object.
  3. Retrieve. jQuery.data(div, "test") returns the same object reference.
  4. 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"
Try It Yourself

How It Works

  1. Write two keys. foo and bar are stored separately.
  2. Full object. $.data(el) returns the element’s data bag.
  3. Your keys. store.foo / store.bar match what you set.
  4. Caution. jQuery may also keep internal keys here — do not assume the object is only yours.

Example 4 — Overwrite and removeData

Update a key, then delete it with $.removeData.

jQuery
var div = document.getElementById("box");

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

  1. Set. First write stores the string "hello".
  2. Overwrite. Setting again with 86 replaces the previous value.
  3. Remove. jQuery.removeData(div, "blah") deletes that key.
  4. Missing key. A later get returns undefined.

Edge Cases & Pitfalls

Check these before relying on jQuery.data() in plugins.

api

Pass a DOM element, not a jQuery object

Unwrap with $el[0]. Passing a collection will not store data on the element you expect.

attributes

Does not read data-* by itself

For HTML5 attributes, use $(el).data() or read .attr("data-…").

undefined

undefined is not a stored value

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

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

Performance Notes

ApproachCostBest for
$.data(el, key, value)O(1)Plugin state on a known element
$(el).data(...)O(1) + convenienceEveryday code and data-*
Huge objects per elementMemoryAvoid storing giant trees if unused

Lookups are cheap. The real cost is holding large objects longer than needed — remove data when widgets tear down.

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.

Next: removeData()

Learn how to clear keys from the jQuery element data store.

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.

9 people found this page helpful