
- HTML - Home
- HTML - Roadmap
- HTML - Introduction
- HTML - History & Evolution
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Headings
- HTML - Paragraphs
- HTML - Fonts
- HTML - Blocks
- HTML - Style Sheet
- HTML - Formatting
- HTML - Quotations
- HTML - Comments
- HTML - Colors
- HTML - Images
- HTML - Image Map
- HTML - Frames
- HTML - Iframes
- HTML - Phrase Elements
- HTML - Code Elements
- HTML - Meta Tags
- HTML - Classes
- HTML - IDs
- HTML - Backgrounds
- HTML Tables
- HTML - Tables
- HTML - Table Headers & Captions
- HTML - Table Styling
- HTML - Table Colgroup
- HTML - Nested Tables
- HTML Lists
- HTML - Lists
- HTML - Unordered Lists
- HTML - Ordered Lists
- HTML - Definition Lists
- HTML Links
- HTML - Text Links
- HTML - Image Links
- HTML - Email Links
- HTML Color Names & Values
- HTML - Color Names
- HTML - RGB & RGBA Colors
- HTML - HEX Colors
- HTML - HSL & HSLA Colors
- HTML - HSL Color Picker
- HTML Forms
- HTML - Forms
- HTML - Form Attributes
- HTML - Form Control
- HTML - Input Attributes
- HTML Media
- HTML - Video Element
- HTML - Audio Element
- HTML - Embed Multimedia
- HTML Header
- HTML - Head Element
- HTML - Adding Favicon
- HTML - Javascript
- HTML Layouts
- HTML - Layouts
- HTML - Layout Elements
- HTML - Layout using CSS
- HTML - Responsiveness
- HTML - Symbols
- HTML - Emojis
- HTML - Style Guide
- HTML Graphics
- HTML - SVG
- HTML - Canvas
- HTML APIs
- HTML - Geolocation API
- HTML - Drag & Drop API
- HTML - Web Workers API
- HTML - WebSocket
- HTML - Web Storage
- HTML - Server Sent Events
- HTML Miscellaneous
- HTML - Document Object Model (DOM)
- HTML - MathML
- HTML - Microdata
- HTML - IndexedDB
- HTML - Web Messaging
- HTML - Web CORS
- HTML - Web RTC
- HTML Demo
- HTML - Audio Player
- HTML - Video Player
- HTML - Web slide Desk
- HTML Tools
- HTML - Velocity Draw
- HTML - QR Code
- HTML - Modernizer
- HTML - Validation
- HTML - Color Picker
- HTML References
- HTML - Cheat Sheet
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - Character Entities
- MIME Media Types
- HTML - URL Encoding
- Language ISO Codes
- HTML - Character Encodings
- HTML - Deprecated Tags
- HTML Resources
- HTML - Quick Guide
- HTML - Useful Resources
- HTML - Color Code Builder
- HTML - Online Editor
HTML - data-* Attribute
HTML data-* attribute is global attribute is used to form a class of attributes that define our own custom data attributes and store custom data in private on the page or application.
Following are the parts of this data attributes
- Attribute Name: It contains one character long, contain no capital letters and be prefixed with 'data-*'.
- Attribute Value: Can be any string.
Syntax
<element data-* = "10784"> ...content </element >
Here "*" can be anything as you will see in the below examples.
Applies On
Most HTML elements support data-* attributes, but there are a few exceptions. Elements that are not typically used to contain content or don't have any intrinsic functionality where custom data attributes would be applicable may not support them. For example <style>, <html>, <title>, <script>, <meta> few tags that not support custom data attribute.
Examples of HTML data-* attribute
Bellow examples will illustrate the HTML data-* attribute, where and how we should use this attribute!
Define custom Data using dat-* Attribute
In the following example we will create a list of itema and define the data-course-type on each item.
<!DOCTYPE html> <html> <head> <title>HTML data-* Attribute</title> </head> <body> <ul> <li data-course-type="frontEnd">HTML</li> <li data-course-type="backEnd">Php</li> <li data-course-type="fullStack">MERN</li> </ul> </body> <html>
Display the custom data on Hovering
In the following example, we are creating an HTML document and using the data-* attribute to embed custom data, as follows −
<!DOCTYPE html> <html> <head> <style> h1 { margin: 0; } ul { margin: 10px 0 0; } li { position: relative; width: 200px; padding-bottom: 10px; } li:after { content: 'Data ID: 'attr(data-id); position: absolute; top: -22px; left: 10px; background: black; color: white; padding: 2px; border: 1px solid #eee; opacity: 0; transition: 0.5s opacity; } li:hover:after { opacity: 1; } </style> </head> <body> <h1>Secret agents</h1> <ul> <li data-id="10784"> Jason Walters, 003: Found dead in "A View to a Kill". </li> <li data-id="97865"> Alex Trevelyan, 006: Agent turned terrorist leader. </li> <li data-id="45732"> James Bond, 007: The main man; shaken but not stirred. </li> </ul> </body> </html>
Use custom data in JavaScript
Let's look into the another example, where we are going to use the data-* attribute to embed custom data. Here data-org-type will store what type of organization in the current li element have.
<!DOCTYPE html> <html> <head> <title>data-* attribute</title> </head> <body> <h1>Organizations</h1> <p>Click on a organization to see what type it is:</p> <ul> <li onclick="showDetails(this)" id="tutorialspoint" data-org-type="EdTech organization"> Tutorialspoint </li> <li onclick="showDetails(this)" id="amazon" data-org-type="E-commerce organization"> Amazon </li> <li onclick="showDetails(this)" id="google" data-org-type="It & Product Based organization"> Google </li> <p id="type"></p> </ul> <script> function showDetails(org) { let orgType = org.getAttribute("data-org-type"); document.getElementById("type").innerHTML = ("The " + org.innerHTML + " is a " + orgType + "."); } </script> </body> </html>
Supported Browsers
Attribute | ![]() |
![]() |
![]() |
![]() |
![]() |
---|---|---|---|---|---|
data-* | Yes 4.0 | Yes 5.5 | Yes 2.0 | Yes 3.1 | Yes 9.6 |