Using HTML5 Data Attributes with JavaScript

Using HTML5 Data Attributes with JavaScript

HTML5 gave us the ability to add arbitrary pieces of information called data attributes to an element to use behind the scenes for any purpose you wish, and here I will demonstrate how to use them in JavaScript. In JavaScript you can access an element’s data attributes for reading and writing using its dataset property which is of DOMStringMap type and is basically a collection of key/value pairs.

Image for post

HTML5 gave us the ability to add arbitrary pieces of information called data attributes to an element to use behind the scenes for any purpose you wish, and here I will demonstrate how to use them in JavaScript.

I put data attributes to good use in my Interactive Periodic Table. There is plenty of information around on the topic but often with simplistic or contrived examples so here I will present a real-world usage with a simplified version of the Periodic Table.

You can add a custom data attribute to an element by hard-coding it into the HTML, prefixing the name with data-, for example:

<div id=”div1" data-manufacturer=”nikon” data-model=”d6">Nikon D6</div>

In JavaScript you can access an element’s data attributes for reading and writing using its dataset property which is of DOMStringMap type and is basically a collection of key/value pairs. The keys are the names used for the attributes but without the data- prefix, so for the above example we would use manufacturer and model, as shown below.

const element = document.getElementById(“div1”);

console.log(element.dataset.manufacturer); // nikon
console.log(element.dataset.model); // d6
element.dataset.manufacturer = “canon”;
element.dataset.model = “EOS 90D”;

It is also easy to find elements with certain data attribute values using querySelector, for example:

const element = document.querySelector(“[data-manufacturer=’nikon’][data-model=’d6']”);

coding web-development programming javascript webdev html5

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

13 Useful JavaScript Developer Tools for Writing High-Quality Code

Today I’m going to show you 13 well-know and popular tools to write better and cleaner JavaScript code. These are tools that I’m using right now on my JavaScript developer journey. So if you want to improve the quality of your projects, this list is for you.

Hire Best JavaScript Developers | JavaScript development company USA

Hire Dedicated JavaScript Developers from a top javascript development agency for your business solutions. We provide JS web developers for hire at a fixed or hourly rate.

6 Ways to Practice Your Coding Skills and Grow Your Career as a JavaScript Developer

6 min read ... Some of the highest-paid JavaScript developers make close to $500k/year ... actually need to learn many languages in the course of their career to ... Learn concepts professional developers need to advance their careers ...