<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Abiola Fasanya</title>
    <description>The latest articles on Forem by Abiola Fasanya (@dev-harbiola).</description>
    <link>https://forem.com/dev-harbiola</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F676164%2F49874531-fd76-4694-8708-bb2d92bd0db9.png</url>
      <title>Forem: Abiola Fasanya</title>
      <link>https://forem.com/dev-harbiola</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/dev-harbiola"/>
    <language>en</language>
    <item>
      <title>Custom Data Attributes in HTML: A Guide to `data-*`</title>
      <dc:creator>Abiola Fasanya</dc:creator>
      <pubDate>Sat, 15 Apr 2023 21:02:56 +0000</pubDate>
      <link>https://forem.com/dev-harbiola/custom-data-attributes-in-html-a-guide-to-data--373</link>
      <guid>https://forem.com/dev-harbiola/custom-data-attributes-in-html-a-guide-to-data--373</guid>
      <description>&lt;h1&gt;
  
  
  Custom Data Attributes in HTML: A Guide to &lt;code&gt;data-*&lt;/code&gt;
&lt;/h1&gt;

&lt;p&gt;When developing web applications, developers often need to add custom data to elements. For example, they may want to associate data with an element for later use in JavaScript or CSS. HTML provides a way to add custom data to elements using attributes that start with "data-". These attributes are called &lt;code&gt;data-*&lt;/code&gt; attributes.&lt;/p&gt;

&lt;h2&gt;
  
  
  What are &lt;code&gt;data-*&lt;/code&gt; attributes?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;data-*&lt;/code&gt; attributes are custom attributes that can be added to HTML elements to store custom data. The "data-" prefix is followed by any name you want to give the attribute. For example, if you want to store a value for a user's name, you can add an attribute like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-name=&lt;/span&gt;&lt;span class="s"&gt;"John"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The attribute &lt;code&gt;data-name&lt;/code&gt; is not a standard HTML attribute, but it's still valid HTML because the "data-" prefix indicates that it's a custom data attribute.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why use &lt;code&gt;data-*&lt;/code&gt; attributes?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;data-*&lt;/code&gt; attributes allow developers to add custom data to elements that can be accessed and manipulated using JavaScript or CSS. They provide a way to store data that isn't visible to users but can be used by developers to perform specific actions or apply styling based on the attribute value.&lt;/p&gt;

&lt;p&gt;Some common use cases for &lt;code&gt;data-*&lt;/code&gt; attributes include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Storing data for JavaScript to access later on.&lt;/li&gt;
&lt;li&gt;Providing metadata about an element that's needed for certain functionality (e.g., a date for a calendar widget).&lt;/li&gt;
&lt;li&gt;Selecting elements for automated testing frameworks like Jest or React Testing Library.&lt;/li&gt;
&lt;li&gt;Applying different themes or styles to an element based on its &lt;code&gt;data-*&lt;/code&gt; attribute value.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Naming conventions for &lt;code&gt;data-*&lt;/code&gt; attributes
&lt;/h2&gt;

&lt;p&gt;It's important to follow naming conventions when creating &lt;code&gt;data-*&lt;/code&gt; attributes to avoid naming conflicts with other HTML attributes or frameworks. Here are some guidelines to keep in mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Attribute names must begin with "data-" followed by any name you want to give the attribute.&lt;/li&gt;
&lt;li&gt;Attribute names should be lowercase and use hyphen-case (i.e., words separated by hyphens, not underscores or camelCase).&lt;/li&gt;
&lt;li&gt;Attribute names should be descriptive to avoid naming conflicts with other libraries.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, here's an example of a &lt;code&gt;data-*&lt;/code&gt; attribute that adheres to these guidelines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;data-user-id=&lt;/span&gt;&lt;span class="s"&gt;"123"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Accessing and manipulating &lt;code&gt;data-*&lt;/code&gt; attributes
&lt;/h2&gt;

&lt;p&gt;To access or manipulate &lt;code&gt;data-*&lt;/code&gt; attributes using JavaScript, you can use the &lt;code&gt;dataset&lt;/code&gt; property on the element. The &lt;code&gt;dataset&lt;/code&gt; property is an object that contains all of the &lt;code&gt;data-*&lt;/code&gt; attributes as properties, with the "data-" prefix removed. For example, if you have an element like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;div&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"my-element"&lt;/span&gt; &lt;span class="na"&gt;data-color=&lt;/span&gt;&lt;span class="s"&gt;"blue"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&amp;lt;/div&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can access the value of the &lt;code&gt;data-color&lt;/code&gt; attribute like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#my-element&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs "blue"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can also set the value of &lt;code&gt;data-*&lt;/code&gt; attributes using JavaScript:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;element&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;#my-element&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="nx"&gt;element&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataset&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;color&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;red&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// Changes data-color to "red"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In CSS, you can target &lt;code&gt;data-*&lt;/code&gt; attributes using attribute selectors. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight css"&gt;&lt;code&gt;&lt;span class="o"&gt;[&lt;/span&gt;&lt;span class="nt"&gt;data-color&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s1"&gt;"blue"&lt;/span&gt;&lt;span class="o"&gt;]&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nl"&gt;background-color&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;blue&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This will apply the &lt;code&gt;background-color&lt;/code&gt; style to any element with a &lt;code&gt;data-color&lt;/code&gt; attribute set to "blue".&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;data-*&lt;/code&gt; attributes provide a way to store custom data in HTML elements that can be accessed and manipulated using JavaScript or CSS. By following naming conventions and using descriptive names, developers can avoid naming conflicts and keep their code organized. &lt;code&gt;data-*&lt;/code&gt; attributes are a powerful tool for front-end development that allow for greater flexibility and customization in web applications.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>programming</category>
      <category>react</category>
      <category>css</category>
    </item>
  </channel>
</rss>
