How to Extract Data Attributes Using jQuery .data()
Extracting custom data attributes from HTML elements is a common task
in modern web development, and jQuery simplifies this process with its
built-in .data() method. This guide covers how to read
individual custom data-* attributes, extract all data
attributes at once as an object, and understand how jQuery automatically
converts data types like numbers, booleans, and JSON strings.
Basic Syntax for Reading a Data Attribute
To read a custom HTML5 data-* attribute, select the
element using jQuery and pass the attribute name (omitting the
data- prefix) into the .data() method.
Given the following HTML element:
<div id="product" data-id="101" data-category="electronics"></div>You can extract the values using:
// Extract by name without the 'data-' prefix
const productId = $('#product').data('id');
const category = $('#product').data('category');
console.log(productId); // Output: 101 (as a number)
console.log(category); // Output: "electronics"Handling Hyphenated Attribute Names
When an attribute contains hyphens, such as
data-user-role or data-item-price, jQuery
automatically converts the name to camelCase. You can access it using
either the camelCase property or the original hyphenated string, though
camelCase is the standard convention.
<div id="user" data-user-role="editor" data-account-status="active"></div>// Recommended: camelCase
const role = $('#user').data('userRole');
// Also valid: hyphenated string
const status = $('#user').data('account-status');
console.log(role); // Output: "editor"
console.log(status); // Output: "active"Extracting All Data Attributes at Once
If an element has multiple data-* attributes and you
want to retrieve all of them simultaneously, call .data()
without passing any arguments. This returns a single JavaScript object
containing key-value pairs of all parsed data attributes.
<button id="action-btn" data-action="delete" data-target-id="45" data-confirm="true"></button>const allData = $('#action-btn').data();
console.log(allData);
// Output: { action: "delete", targetId: 45, confirm: true }Automatic Type Casting
Unlike .attr('data-...') which always returns a string,
jQuery's .data() method attempts to parse values into their
corresponding JavaScript data types:
- Numbers:
"42"becomes the number42. - Booleans:
"true"or"false"become booleantrueorfalse. - Null:
"null"becomesnull. - JSON: Valid JSON strings are automatically parsed into JavaScript objects or arrays.
<div id="meta" data-count="15" data-valid="false" data-tags='["news", "updates"]'></div>console.log(typeof $('#meta').data('count')); // Output: "number"
console.log(typeof $('#meta').data('valid')); // Output: "boolean"
console.log(Array.isArray($('#meta').data('tags'))); // Output: true.data() vs. .attr()
Use .data() when you want jQuery to automatically cache
the value and parse types. If you specifically need to read the exact
string value directly from the DOM without type conversion or caching,
use .attr('data-your-key') instead.