HTML Basic
HTML Reference
- HTML Tags
- HTML Deprecated Tags
- HTML Events
- HTML Attributes
- accept
- accept-charset
- accesskey
- action
- align
- alt
- as
- async
- autocomplete
- autofocus
- autoplay
- bgcolor
- border
- charset
- checked
- cite
- class
- color
- cols
- colspan
- content
- contenteditable
- controls
- coords
- data
- data-*
- datetime
- default
- defer
- dir
- dirname
- disabled
- download
- draggable
- enctype
- enterkeyhint
- for
- form
- formaction
- headers
- height
- hidden
- high
- href
- hreflang
- http-equiv
- id
- inert
- inputmode
- ismap
- kind
- label
- lang
- list
- loop
- low
- max
- maxlength
- media
- method
- min
- multiple
- muted
- name
- novalidate
- onabort
- onafterprint
- onbeforeprint
- onbeforeunload
- onblur
- oncanplay
- oncanplaythrough
- onchange
- onclick
- oncontextmenu
- oncopy
- oncuechange
- oncut
- ondblclick
- ondrag
- ondragend
- ondragenter
- ondragleave
- ondragover
- ondragstart
- ondrop
- ondurationchange
- onemptied
- onended
- onerror
- onfocus
- onhashchange
- oninput
- oninvalid
- onkeydown
- onkeypress
- onkeyup
- onload
- onloadeddata
- onloadedmetadata
- onloadstart
- onmousedown
- onmousemove
- onmouseout
- onmouseover
- onmouseup
- onmousewheel
- onoffline
- ononline
- onpagehide
- onpageshow
- onpaste
- onpause
- onplay
- onplaying
- onpopstate
- onprogress
- onratechange
- onreset
- onresize
- onscroll
- onsearch
- onseeked
- onseeking
- onselect
- onstalled
- onstorage
- onsubmit
- onsuspend
- ontimeupdate
- ontoggle
- onunload
- onvolumechange
- onwaiting
- onwheel
- open
- optimum
- pattern
- placeholder
- popover
- popovertarget
- popovertargetaction
- poster
- preload
- readonly
- rel
- required
- reversed
- rows
- rowspan
- sandbox
- scope
- selected
- shape
- size
- sizes
- span
- spellcheck
- src
- srcdoc
- srclang
- srcset
- start
- step
- style
- tabindex
- target
- title
- translate
- type
- usemap
- value
- width
- wrap
- HTML Global Attributes
- HTML Status Code
- HTML Language Code
- HTML Country Code
- HTML Charset
- MIME Types
HTML data-* Attribute
Photo Credit to CodeToFun
🙋 Introduction
The data-*
attribute in HTML provides a way to store custom data private to the page or application. It allows you to associate additional information with HTML elements without affecting the document's standard rendering.
This attribute is especially useful when you need to store data that might be used by JavaScript or CSS.
🎯 Purpose of data-*
The primary purpose of the data-*
attribute is to store custom data private to the page or application. This data is not displayed to users and doesn't affect the appearance of the document. Instead, it serves as a convenient way to embed extra information within HTML elements.
💡 Syntax
The data attribute is prefixed with "data-" followed by a name that represents the custom data attribute. For example:
<div data-example="custom-data">This element has custom data</div>
🧠 How it Works
In this example, the data-example attribute is used to store the custom data "custom-data" for the div element.
💎 Values
The data-*
attribute can have various values depending on the type of information you want to store. It's common to use string values, but you can also store numbers, Booleans, or even JSON data.
📄 Example
Let's look at a simple example of how to use the data-*
attribute in HTML:
<div id="user" data-user-id="123" data-username="john_doe" data-email="john@example.com">
<!-- Content for the user profile -->
</div>
🧠 How it Works
In this example, the data-*
attribute is used to store user-related information, such as user ID, username, and email address. This data can be accessed and manipulated using JavaScript.
🔄 Dynamic Values with JavaScript
You can dynamically set or retrieve values for the data-*
attribute using JavaScript. This is particularly useful when you need to update the custom data based on user interactions or other dynamic events. Here's a brief example:
<script>
// Get the value of the data attribute
var userId = document.getElementById("user").dataset.userId;
// Set a new value for the data attribute
document.getElementById("user").dataset.username = "new_username";
</script>
🧠 How it Works
In this script, we are using the dataset property to access and modify the values of the data-*
attributes for the element with the ID "user."
🏆 Best Practices
- Choose meaningful and descriptive names for your custom
data-*
attributes to maintain clarity in your code. - Avoid storing sensitive or critical information in the
data-*
attributes, as they are easily accessible by users through browser developer tools. - Use the dataset property in JavaScript to interact with
data-*
attributes efficiently.
🎉 Conclusion
The data-*
attribute in HTML is a powerful tool for storing custom data within elements, providing a flexible and convenient way to enhance the functionality of your web pages or applications.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (HTML data-* Attribute), please comment here. I will help you immediately.