jQuery.data() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Utilities

Definition

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.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$.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
CallWhat it does
jQueryLearn jQuery.data(): store and read arbitrary values on DOM elements, objects and numbers, full data store, removeData, vs .data() and data-*, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Get a native element: document.getElementById("box") or $("#box")[0].
Step 2Set with $.data(el, "key", value) — any type except undefined.
Step 3Read with $.data(el, "key"), or $.data(el) for the whole store.

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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: jQuery.removeData() Method

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

jQuery.removeData() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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