HTML Custom Data Attributes, also known as data-* attributes, are one of the most powerful and widely used features in modern web development. They allow developers to store extra information directly inside HTML elements without using non-standard attributes or embedding the data in the DOM in confusing ways. These attributes play a major role in JavaScript DOM manipulation, frontend frameworks, SEO-friendly HTML structuring, and dynamic UI/UX development. In this document, you will learn everything about HTML Custom Data Attributes with explanations, benefits, advanced usage, JavaScript examples, best practices, and full working code with outputs.
HTML Custom Data Attributes follow a specific format: data-name="value" They must always begin with data- and you can name them anything you wish. These attributes help store private or custom information in an element for JavaScript to use later.
Example:
User Info
Output:
User Info
Browsers ignore these attributes stylistically, but JavaScript can access them easily using the dataset property.
Sometimes, you need extra data on an elementβlike product price, user id, animation speed, etc. Data attributes store such information cleanly and safely.
Search engines ignore data attributes. This prevents keyword stuffing issues but allows structured storage of extra semantic data.
Instead of writing hidden inputs or complex JSON inside HTML, you can use simple data attributes.
Many libraries and frameworks like Bootstrap, jQuery, Alpine.js, and even React-friendly plugins use data attributes for configuration.
Content
Examples:
Laptop
Output:
Laptop
JavaScript gives a special property called dataset to access data attributes.
Click Me
Output:
Hello World!
Product A
Output:
599
If an attribute is named data-user-name, JavaScript will access it as:
dataset.userName
Output:
nila
Running Shoes
Output:
1001
1499
Nike
Move Box
Output:
3
left
Output:
delete
224
You can even use data attributes in CSS using attribute selectors.
Active User
Output:
Active User
- Red Text
- Blue Text
- Green Text
Output:
Red Text (in red)
Blue Text (in blue)
Green Text (in green)
Although values must be strings, you can store JSON as a string and parse it with JavaScript.
Output:
{name: "nila", age: 21}
HTML Custom Data Attributes are essential for modern web development. They provide a clean, SEO-friendly, and structured way to store custom information inside HTML elements. JavaScriptβs dataset property makes them extremely easy to access, modify, and use for dynamic UI and interactive experiences. From e-commerce websites, animations, UI frameworks, configuration attributes, and interactivity, data-* attributes are incredibly useful in almost any frontend scenario.
Use the <link> tag inside the <head> to attach an external CSS file.
Comments in HTML are written between <!-- and -->.
HTML entities are used to display reserved or special characters.
The <iframe> tag embeds another webpage within the current page.
The id attribute uniquely identifies a single HTML element.
Hyperlinks are created using the <a> tag with an href attribute.
Use the <img> tag and specify the image source with the src attribute.
Use the target="_blank" attribute inside the <a> tag.
Copyrights © 2024 letsupdateskills All rights reserved