HTML data-* Global Attribute
The data-* attributes store custom data on any element. The values can be read in JavaScript with element.dataset and used in CSS attribute selectors.
Description
The data-* global attribute in HTML allows you to store custom data private to the page or application. The attribute name must start with data-, followed by at least one character, and must not contain uppercase ASCII letters. The attribute value can be any string.
The data-* attributes are part of the HTML standard and are supported by all browsers. They are not displayed on the page, but they are visible in the page source, so they are not a place for secrets. In JavaScript they are available through the element's dataset property, where the name is converted to camelCase: data-product-id becomes element.dataset.productId. In CSS they can be used in attribute selectors such as [data-state="open"] and with the attr() function.
Here are some examples of how to use the data-* attribute:
<div data-product-id="1234567890" data-product-name="My Product">
<h1>My Product</h1>
</div>
<button data-action="submit-form">Submit Form</button>
<img data-src="https://example.com/image.png" alt="Image">
In the first example, we are using the data-* attribute to store the product ID and name. This data can then be accessed from JavaScript to populate the page with dynamic content.
In the second example, we are using the data-* attribute to store the action that the button should perform when it is clicked. This data can then be read by JavaScript and used to submit the form.
In the third example, we are using the data-* attribute to store the source of the image. This data can then be used by JavaScript to load the image dynamically.
The data-* attributes can be used to store any type of data that you need to access from JavaScript. This makes them a very versatile tool for web development.
Here are some tips for using the data-* attribute:
- Use descriptive names for your
data-*attributes. This will make it easier to understand what the data is and how it is being used. - Avoid using the
data-*attribute to store data that is sensitive or private. Anyone can read the value in the page source. - Be aware that the
data-*attributes are not validated by the browser. This means that you need to make sure that the data is valid before using it in JavaScript.
Overall, the data-* global attribute is a powerful tool that can be used to store custom data in HTML. This data can then be accessed from JavaScript to create more dynamic and interactive web pages.
Syntax
<element data-*="value">
Values
- *This value must not contain capital letters, and must also contain at least one character after the prefix data-. The attribute value can be any string.
- valueThis value represents the data that is stored.
Example
Browser Support
The following information will show you the current browser support for the HTML data-* global attribute. Hover over a browser icon to see the version that first introduced support for this HTML global attribute.
Desktop





Tablets & Mobile





