Open In App
Related Articles

HTML data-* Attributes

Improve Article
Save Article
Like Article

Custom Data Attributes allow you to add your own information to tags in HTML. Even though the name suggests otherwise, these are not specific to HTML5 and you can use the data-* attribute on all HTML elements. 

The data-* attributes can be used to define our own custom data attributes. It is used to store custom data in private to the page or application. 
There are mainly 2 parts of the Data Attributes: 

  1. Attribute Name: Must be at least one character long, contain no capital letters and be prefixed with ‘data-‘.
  2. Attribute Value: Can be any string.


<li data-book-author="Rabindra Nath Tagore"> Gitanjali </li>



<!DOCTYPE html>
        function showDetails(book) {
            let bookauthor = book.getAttribute("data-book-author");
            alert(book.innerHTML + " is written by "
                                    + bookauthor + ".");
    <p>Click on the book name to know author's name :</p>
        <li onclick="showDetails(this)"
            data-book-author="Rabindra Nath Tagore">
        <li onclick="showDetails(this)"
            data-book-author="Mahatma Gandhi">
            Conquest of Self
        <li onclick="showDetails(this)"
            data-book-author="Sarojini Naidu">
            Broken Wings


When we click on the book, we can see the name of the author in a separate dialogue box. 

Supported Browsers: The browser supported by HTML | data-* Attribute  are listed below: 

  • Google Chrome
  • Edge 12 and above
  • Firefox
  • Safari
  • Opera

Last Updated : 02 Aug, 2023
Like Article
Save Article
Similar Reads
Related Tutorials