Advanced15 min read

Custom Data Attributes

Learn how to use data-* attributes to store custom information on HTML elements and access it with JavaScript's dataset API.

What Are data-* Attributes?

HTML allows you to add custom attributes to any element by prefixing them with data-. These are called data attributes and provide a standard way to embed extra information in your markup.

html
<button data-action="delete" data-item-id="42">Delete</button>

Data attributes:

  • Are completely ignored by the browser's rendering engine.
  • Do not affect semantics or accessibility (unless read by JavaScript).
  • Are valid HTML — the data-* pattern is part of the specification.
  • Can store any string value.

Naming Conventions

Data attribute names must follow these rules:

  1. Start with data-.
  2. Contain only lowercase letters, numbers, hyphens, dots, colons, and underscores.
  3. No uppercase letters.

Common naming patterns

AttributePurpose
data-idElement identifier
data-typeElement category/type
data-statusCurrent state (active, disabled, etc.)
data-tooltipTooltip text
data-sort-keySorting value
data-configConfiguration (JSON string)

Keep names descriptive but concise. Use hyphens to separate words: data-user-role, not data-userrole.

The dataset API

JavaScript provides the dataset property on every element to read and write data attributes. Hyphenated names are converted to camelCase:

html
<div id="user" data-user-name="Alice" data-role="admin" data-login-count="5"></div>
js
const el = document.getElementById('user');

// Read
console.log(el.dataset.userName);   // "Alice"
console.log(el.dataset.role);        // "admin"
console.log(el.dataset.loginCount);  // "5" (always a string)

// Write
el.dataset.status = 'online';  // adds data-status="online"

// Delete
delete el.dataset.role;  // removes data-role attribute

Note that dataset values are always strings. Parse them with parseInt(), JSON.parse(), etc. as needed.

Use Cases and Limitations

Good use cases

  • UI state management: data-active="true", data-tab="settings"
  • JavaScript hooks: data-action="save" for event delegation
  • CSS targeting: [data-theme="dark"] { ... }
  • Filtering/sorting: data-price="29.99", data-category="books"

When NOT to use data attributes

  • Accessibility information — Use ARIA attributes (aria-label, aria-expanded, etc.) instead.
  • Styling hooks only — Prefer CSS classes. Data attributes should carry data, not just be styling hooks.
  • Large amounts of data — For complex data, store it in JavaScript objects and reference elements by ID.
  • Sensitive data — Data attributes are visible in the DOM. Never store passwords, tokens, or personal information.

Practical Example

html
<!-- Product cards with data attributes -->
<div class="product-list">
  <div class="card" data-product-id="101" data-category="electronics" data-price="299.99">
    <h3>Wireless Headphones</h3>
    <p>Price: $299.99</p>
    <button data-action="add-to-cart" data-product-id="101">Add to Cart</button>
  </div>

  <div class="card" data-product-id="102" data-category="books" data-price="19.99">
    <h3>HTML & CSS Guide</h3>
    <p>Price: $19.99</p>
    <button data-action="add-to-cart" data-product-id="102">Add to Cart</button>
  </div>
</div>

<!-- CSS can target data attributes -->
<style>
  [data-category="electronics"] { border-left: 3px solid #3498db; }
  [data-category="books"] { border-left: 3px solid #2ecc71; }
</style>

How do you access the `data-user-name` attribute in JavaScript?

Ready to practice?

Create your free account to access the interactive code editor, run challenges, and track your progress.