SyntaxStudy
Sign Up
jQuery Beginner 3 min read

jQuery .data() Method

.data() Method

jQuery's .data() method stores and retrieves arbitrary data on DOM elements without modifying the HTML. Data is stored in jQuery's internal cache, not in the DOM.

Example
// Store data
$("#userCard").data("userId", 42);
$("#userCard").data("role", "admin");

// Retrieve data
const id   = $("#userCard").data("userId"); // 42
const role = $("#userCard").data("role");   // "admin"

// Remove data
$("#userCard").removeData("userId");
Pro Tip

.data() stores any JavaScript type (objects, arrays) — not just strings like data attributes.