data- prefix, followed by the name of the attribute.
Built-in data attributes are attributes that are predefined by the HTML specification and have specific meanings and behaviors. Some examples of built-in data attributes are
data-title. These attributes are used by the browser or by third-party libraries to store and access data on elements.
<!-- Define a custom data attribute on an element --> <div id="my-element" data-custom-data="hello, world"></div> <script> // Get a reference to the element const element = document.getElementById("my-element"); // Get the value of the custom data attribute const customData = element.dataset.customData; // Output the value of the custom data attribute console.log(customData); // Output: "hello, world" </script>
In this code, a custom data attribute named
data-custom-data is defined on the
div element. The
I hope this helps! Let me know if you have any questions.