Custom Data Attributes data-*
Store custom data directly on elements with data-* attributes.
Custom Data Attributes data-* is a free HTML Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What Are data-* Attributes?
Custom data attributes let you store extra information directly on HTML elements:
<button data-user-id="42" data-action="delete">Delete User</button>
<li data-category="frontend" data-level="beginner">HTML</li>
<!-- Format: data-[name]="value" -->
<!-- Any alphanumeric name after data- is valid -->
<!-- Values are always strings -->Why data-* Exists
Before data-*, developers stored extra data in:
- Hidden inputs (messy)
- CSS class names (fragile)
- Non-standard attributes like
relortitle(invalid HTML)
HTML5 formalized data-* as the official way to embed custom data without affecting semantics or presentation.
Naming Rules
data-* attribute naming rules:
- Must start with
data- - Name can contain lowercase letters, digits, hyphens, underscores, periods
- No uppercase letters in the name (the HTML spec lowercases them)
- Name cannot start with
xml
<div data-user-id="1">OK</div>
<div data-product-category="tools">OK</div>
<!-- <div data-XML-thing="1">BAD: starts with xml -->
<!-- <div data-MyProp="1">BAD: uppercase in name -->Reading data-* in JavaScript
Access custom data via the dataset property:
<button id="btn"
data-user-id="42"
data-action="delete"
data-confirm-message="Are you sure?"
>
Delete
</button>
<script>
const btn = document.getElementById('btn');
console.log(btn.dataset.userId); // '42'
console.log(btn.dataset.action); // 'delete'
console.log(btn.dataset.confirmMessage); // 'Are you sure?'
// Note: data-user-id → dataset.userId (camelCase conversion)
</script>Writing data-* with dataset
Set and remove data attributes via dataset:
const card = document.querySelector('.card');
// Set:
card.dataset.status = 'active';
// Now: <div class="card" data-status="active">
// Delete:
delete card.dataset.status;
// Now: data-status is removed
// Alternatively with setAttribute:
card.setAttribute('data-status', 'inactive');
card.removeAttribute('data-status');Hyphen to camelCase Conversion
The dataset API converts hyphen-separated names to camelCase:
<!-- HTML attribute JavaScript dataset property -->
data-user-id → dataset.userId
data-product-category → dataset.productCategory
data-max-retries → dataset.maxRetries
data-is-active → dataset.isActiveCSS Attribute Selectors
Use data attributes in CSS selectors:
[data-status="active"] {
color: green;
}
[data-status="inactive"] {
color: gray;
text-decoration: line-through;
}
/* Present but any value: */
[data-featured] {
border: 2px solid gold;
}Tooltip Pattern
Build pure-CSS tooltips with data attributes:
<span data-tooltip="This is a tooltip">Hover me</span>
/* CSS: */
/*
[data-tooltip] {
position: relative;
}
[data-tooltip]::after {
content: attr(data-tooltip); /* attr() reads the attribute value */
position: absolute;
background: #333;
color: white;
padding: 0.25rem 0.5rem;
border-radius: 4px;
white-space: nowrap;
opacity: 0;
pointer-events: none;
transition: opacity 0.2s;
}
[data-tooltip]:hover::after { opacity: 1; }
*/State Tracking Pattern
Track component state with data attributes:
<button data-state="off" id="toggle">Toggle</button>
<script>
document.getElementById('toggle').addEventListener('click', function() {
const state = this.dataset.state;
this.dataset.state = state === 'off' ? 'on' : 'off';
this.textContent = this.dataset.state === 'on' ? 'Active' : 'Toggle';
});
</script>
/* CSS: */
/* [data-state="on"] { background: green; color: white; } */data-* vs JavaScript Variables
When to use data-* vs storing data in JavaScript:
- Use data-* for values that correspond directly to a DOM element and may be read by CSS
- Use JavaScript variables/objects for complex data structures not tied to specific elements
- Do not store large or sensitive data in data-* — it is visible in DevTools
data-* Is Not for Presentation
Common mistakes to avoid:
<!-- BAD: storing presentation data in data-* -->
<div data-color="red" data-font-size="18">Text</div>
<!-- Use CSS classes instead: -->
<div class="text-danger text-lg">Text</div>
<!-- BAD: storing sensitive data -->
<div data-api-key="sk-123...">Content</div>
<!-- NEVER store secrets in HTML attributes -->Quick Check
How does the dataset API access a data-product-id attribute?
Recap: data-* Attributes
Custom data attribute essentials:
data-name="value"— store custom metadata on elements- Accessed via
element.dataset.camelCase - Hyphens convert to camelCase in dataset API
- Use in CSS with
[data-name="value"]selectors - Never store sensitive data (keys, passwords) in data attributes
Frequently asked questions
Is the “Custom Data Attributes data-*” lesson free?
Yes — the full text of “Custom Data Attributes data-*” is free to read here on the web, and the HTML Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the HTML Academy course, upgrade to CoddyKit PRO.
What will I learn in “Custom Data Attributes data-*”?
Store custom data directly on elements with data-* attributes. You practise HTML Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start HTML Academy?
No prior experience is required. HTML Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Custom Data Attributes data-*” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this HTML Academy lesson?
Yes. Every HTML Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- id class style and title
- The hidden Attribute
- tabindex and accesskey
- Custom Data Attributes data-*