- HTML Tutorial
- HTML - Home
- HTML - History and Evolution
- HTML - Overview
- HTML - Editors
- HTML - Basic Tags
- HTML - Elements
- HTML - Attributes
- HTML - Formatting
- HTML - Headings
- HTML - Paragraphs
- HTML - Quotations
- HTML - Comments
- HTML - Phrase Tags
- HTML - Meta Tags
- HTML - Style Sheet
- HTML - CSS Classes
- HTML - CSS IDs
- HTML - Images
- HTML - Image Map
- HTML Tables
- HTML - Tables
- HTML - Headers & Caption
- 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 - Iframes
- HTML - Blocks
- HTML Backgrounds
- HTML - Backgrounds
- HTML Colors
- HTML - Colors
- HTML - RGB
- HTML - HEX
- HTML - HSL
- 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 - 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 Code Builder
- HTML References
- HTML - Tags Reference
- HTML - Attributes Reference
- HTML - Events Reference
- HTML - Fonts Reference
- HTML - ASCII Codes
- ASCII Table Lookup
- HTML - Color Names
- HTML - 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 - Global data-* Attribute
The data-* attribute is an HTML 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
Following is the syntax of the data-* attribute −
<element data-id = "10784"> ...content </element >
Here "id" is optional; we can use other characters instead of id.
Example
In the following example, we are creating an HTML document and using the data-* attribute to embed custom data, as follows −
<!DOCTYPE html> <html> <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; James' nemesis in "Goldeneye".</li> <li data-id="45732">James Bond, 007: The main man; shaken but not stirred.</li> </ul> </body>undefined undefined </html>
On runnig the above code, it will generate an output consisting of thetext in bullets format displayed on the webpage.
Example
Let's look into the another example, where we are going to use the data-* attribute to embed custom data.
<!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>
When we execute the above script, it will generate an output consisting of the text dispplayed in the bullet format on the webpage. when the user clicks on the amazon text it displays the embed text.
To Continue Learning Please Login
Login with Google