HTML5 data-* Attributes
1. What Are data-* Attributes?
Data attributes let you embed custom data into any HTML element using the syntax data-name="value". This data doesn't show on the page but is accessible through JavaScript, making it perfect for dynamic UI logic.
2. Syntax and Example
<div id="fruitBox" data-fruit="apple" data-color="red">
Click me to reveal the fruit!
</div>
<script>
const box = document.getElementById("fruitBox");
box.addEventListener("click", () => {
const fruit = box.dataset.fruit;
const color = box.dataset.color;
alert("You clicked a " + color + " " + fruit);
});
</script>
[ Clicking the box shows “You clicked a red apple” ]
3. Accessing data-* in JavaScript
Each data-* attribute becomes a property on the dataset object. The name after data- becomes camelCased.
<div data-fruit-type="banana" data-ripeness-level="high" id="bananaBox"></div>
<script>
const bananaBox = document.getElementById("bananaBox");
const type = bananaBox.dataset.fruitType;
const ripeness = bananaBox.dataset.ripenessLevel;
console.log(type, ripeness); // banana, high
</script>
4. Setting Data with JavaScript
You can also update or add new data attributes via JavaScript:
bananaBox.dataset.ripenessLevel = "medium";
bananaBox.dataset.source = "farm42";
5. Real-World Use Cases
- Storing product IDs in e-commerce listings
- Attaching metadata to list items, cards, or rows
- Passing config or state to JavaScript logic
- Identifying user-selected items like apples, bananas, cherries 🍎🍌🍒
6. Best Practices
- Use lowercase and hyphens in HTML (e.g.,
data-user-id) - Use camelCase to access in JavaScript (
dataset.userId) - Don’t abuse data attributes to store large or sensitive data
- Keep them semantic and purposeful — they’re not just for “hacks”
7. Interactive Example: Toggle Favorite
<button data-fruit="cherry" data-favorite="false" onclick="toggleFavorite(this)">
Cherry 🍒 - Favorite: No
</button>
<script>
function toggleFavorite(btn) {
const isFav = btn.dataset.favorite === "true";
btn.dataset.favorite = !isFav;
btn.innerText = `${btn.dataset.fruit} 🍒 - Favorite: ${!isFav ? "Yes" : "No"}`;
}
</script>
[ Clicking the button toggles favorite state dynamically ]
Summary
HTML5 data attributes offer a clean and flexible way to embed extra information into HTML — information that JavaScript can act upon. You’ve learned how to:
- Add custom
data-*attributes to any element - Access and update those attributes with
dataset - Use them for clean, maintainable dynamic behaviors
Check your understanding 1
Check your understanding 2
Check your understanding 3
Choose the option that lists all and only the correct statements.
<div data-student-id='12345'></div><input data-phone-number='9876543210' /><span data-user='user@example.com'><button data-click='true'>Click Me</button>
Check your understanding 4
Check your understanding 5
Check your understanding 6
Choose the option that lists all and only the correct statements.
- Use descriptive and meaningful names for data attributes
- Avoid storing sensitive information in data attributes
- Keep data values small to optimize page performance
- Use data attributes to style elements directly with CSS
What’s Next?
Next up: Let’s build a dynamic component (like a fruit filter or quiz) entirely powered by data-* attributes and JavaScript — no backend needed.
Reaching the recap and completing the quizzes are separate milestones.