Custom Data Storage

Data Attributes

Store custom data directly in HTML elements using data-* attributes. Perfect for configuration, state management, and JavaScript integration without polluting the global namespace.

What are data attributes in HTML?

data-* attributes store custom information on any element, for example <button data-product-id="42">. JavaScript reads them through element.dataset (button.dataset.productId), and CSS can select or display them with [data-state="open"] and attr(). They're the standard way to attach extra data without inventing invalid attributes.

<li data-id="42" data-status="done">Write docs</li>

const li = document.querySelector("li");
li.dataset.id;          // "42"
li.dataset.status = "todo";

li[data-status="done"] { text-decoration: line-through; }

HTML5 Data Attributes

Data attributes provide a standardized way to store custom data in HTML elements. They are perfect for passing configuration data to JavaScript, storing state information, and creating flexible, maintainable web applications.

JavaScript Ready

Easy access via dataset API

Valid HTML

Standard HTML5 specification

Flexible Storage

Store any type of data

Data Attribute Syntax & Rules

HTML Syntax

Basic: data-name="value"

Camel Case: data-user-id="123"

Complex: data-config='{"key": "value"}'

Boolean: data-active="true"

Numbers: data-count="42"

JavaScript Access

Dataset: element.dataset.name

getAttribute: getAttribute("data-name")

Camel Case: data-user-id → dataset.userId

Set Data: dataset.name = "value"

Remove: delete dataset.name

Data Attribute Naming Rules

  • • Must start with "data-"
  • • Only lowercase letters, numbers, hyphens
  • • No uppercase letters in HTML
  • • Hyphens convert to camelCase in JS
  • • Cannot contain special characters
  • • Should be descriptive and meaningful

Complete Data Attributes Implementation

Comprehensive demonstration of data attributes for configuration, state management, tracking, theming, and progressive enhancement with full JavaScript integration.

Code Editor

<ul id="products">
  <li data-id="101" data-price="19.99" data-category="book">HTML Book</li>
  <li data-id="102" data-price="4.50" data-category="sticker">CSS Sticker</li>
  <li data-id="103" data-price="29.00" data-category="book">JS Book</li>
</ul>
<p id="info">Click a product.</p>

Preview

Use Cases & Best Practices

Perfect Use Cases

  • • Component configuration
  • • State management
  • • Analytics tracking
  • • CSS targeting
  • • API parameters
  • • Form validation rules
  • • Progressive enhancement
  • • A/B testing variants

Best Practices

  • • Use meaningful, descriptive names
  • • Keep data minimal and focused
  • • Validate data types in JavaScript
  • • Use JSON for complex objects
  • • Consider performance impact
  • • Document your data schema
  • • Test across browsers
  • • Provide fallback mechanisms

Data Attributes Checklist

  • Follow HTML5 naming conventions
  • Use dataset API for JavaScript access
  • Validate and sanitize data
  • Consider accessibility implications
  • Document data attribute usage
  • Test performance with large datasets

Data Attributes — step by step

1 / 4
1

Add a data attribute to an element

Any data-* attribute stores custom data tied to an element. Use lowercase kebab-case names like data-user-id='42' or data-product-name='shirt'.

1 of 4
🔍data-* vs Custom Properties
Use data-* attributes when data is tied to a specific element in the DOM. Use CSS custom properties (--var) when data drives visual styling. Use JavaScript variables when data is not tied to the DOM at all. Each has its own domain.

🎯 Quick Check

How do you access data-product-id='99' in JavaScript?

HTML Data Attributes: summary

Key points

  • Hyphenated names become camelCase in dataset: data-user-name becomes dataset.userName.
  • Values are always strings; convert them with Number() or JSON.parse() as needed.
  • Don't store sensitive data in them; anyone can read them in the page source.

Frequently asked questions

›How do I get a data attribute value in JavaScript?

Use element.dataset.name for data-name, or element.getAttribute("data-name").

›Are data attributes good for SEO?

Search engines ignore them. They're for your scripts and styles, not for content.