Skip to content

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • Asp.Net Core
  • C#
  • SQL
  • JavaScript
  • CSS
  • About
  • ErcanOPAK.com
  • No Access
  • Privacy Policy
HTML

HTML: Use data-* Attributes to Store Custom Data

- 20.06.26 - ErcanOPAK

📦 Store Extra Data in HTML Elements

Need to store data without showing? data-* attributes store custom data. Access via JavaScript. No hidden input needed.

📝 Basic Data Attributes

<!-- Store IDs and metadata -->
<div class="product" data-id="123" data-category="electronics" data-price="199.99">
  <h3>Product Name</h3>
  <button class="add-to-cart">Add to Cart</button>
</div>

<!-- Store user data -->
<div class="user-profile" data-user-id="42" data-role="admin">
  <h2>John Doe</h2>
</div>

<!-- Store configuration -->
<div class="widget" data-config='{"theme":"dark","position":"fixed"}'></div>

🎯 Access in JavaScript

<script>
// Get data attribute
const product = document.querySelector('.product');
const productId = product.dataset.id;  // '123'
const category = product.dataset.category;  // 'electronics'
const price = parseFloat(product.dataset.price);  // 199.99

// Set data attribute
product.dataset.discount = '0.15';

// Check if attribute exists
if (product.dataset.hasOwnProperty('category')) {
  console.log('Category:', product.dataset.category);
}

// Click handler
document.querySelector('.add-to-cart').addEventListener('click', (e) => {
  const productDiv = e.target.closest('.product');
  const id = productDiv.dataset.id;
  const price = productDiv.dataset.price;
  addToCart(id, price);
});

// JSON data
const config = JSON.parse(document.querySelector('.widget').dataset.config);
console.log(config.theme);  // 'dark'

// Multiple data attributes with dashes
// data-user-id → dataset.userId (camelCase)
<div data-user-id="42"></div>
const userId = div.dataset.userId;  // '42'
</script>

💡 Use Cases

  • Store item IDs for event handlers
  • Pass data to JavaScript without AJAX
  • Server-side rendering with client-side state
  • Configuration for widgets and components
  • Analytics tracking (data-track-* )

“Used hidden input for product ID. Switched to data-id. Cleaner, semantic, accessible via dataset. Data attributes are perfect for storing metadata.”

— Frontend Developer

Related posts:

HTML5: Use for Autocomplete Without JavaScript

How to Solve The 'Object reference not set to an instance of an object' error in Batch Edit mode for...

Defaults to submit

Post Views: 5

Post navigation

CSS: Use Transform for Animations and Layout
JavaScript: Understand Closures for Powerful Functions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

July 2026
M T W T F S S
 12345
6789101112
13141516171819
20212223242526
2728293031  
« Jun    

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)
  • How to enable, disable and check if Service Broker is enabled on a database in SQL Server (597)
  • Add Constraint to SQL Table to ensure email contains @ (582)
  • Average of all values in a column that are not zero in SQL (545)
  • How to use Map Mode for Vertical Scroll Mode in Visual Studio (512)
  • Find numbers with more than two decimal places in SQL (460)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes
  • Ajax: Use Axios for HTTP Requests
  • JavaScript: Understand Hoisting
  • HTML: Use Web Storage for Client-Side Data
  • CSS: Use Filter Effects for Visual Magic
  • Windows 11: Unlock God Mode for All Settings

Most Viewed Posts

  • Get the User Name and Domain Name from an Email Address in SQL (960)
  • How to add default value for Entity Framework migrations for DateTime and Bool (898)
  • How to make theater mode the default for Youtube (857)
  • Get the First and Last Word from a String or Sentence in SQL (840)
  • How to select distinct rows in a datatable in C# (814)

Recent Posts

  • C#: Use Using Statements for Resource Management
  • C#: Use Lambda Expressions for Concise Code
  • SQL: Use GROUP BY for Data Aggregation
  • .NET Core: Master Routing for Clean URLs
  • Git: Use Reset to Undo Local Changes

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com