Front-end Tutorials

Front-end Tutorials

HTMLCSSSassJavaScriptReactJS
CMS Tutorials

CMS Tutorials

WordPress
Tutorials expand

HTML Basic

HTML Reference

HTML data-* Attribute

Posted in HTML Tutorial
Updated on Sep 22, 2024
By Mari Selvan
👁️ 71 - Views
⏳ 4 mins
💬 1 Comment
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:

data-*.html
Copied
Copy To Clipboard
<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:

data-*.html
Copied
Copy To Clipboard
<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:

data-*.html
Copied
Copy To Clipboard
<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:

To get interesting news and instant updates on Front-End, Back-End, CMS and other Frameworks. Please Join the Telegram Channel:

Author

author
👋 Hey, I'm Mari Selvan

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

Share Your Findings to All

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Mari Selvan
Mari Selvan
9 months ago

If you have any doubts regarding this article (HTML data-* Attribute), please comment here. I will help you immediately.

We make use of cookies to improve our user experience. By using this website, you agree with our Cookies Policy
AgreeCookie Policy