Skip to main content

How to Read and Set data-* Attributes with dataset in JavaScript

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Attributes whose names start with data-, such as data-price="4.50", are yours to fill with any information. In JavaScript, element.dataset holds all of them: element.dataset.price reads data-price, and assigning element.dataset.size = 'large' adds data-size="large" to the element.

Syntax​

Syntax (HTML)
<element data-<name>="<value>">
Syntax (JavaScript)
<element>.dataset.<camelCaseName>
<element>.dataset.<camelCaseName> = <value>;
PartWhat it does
data-<name>The attribute in the HTML. <name> is lowercase, with dashes between words, such as data-in-stock.
<value>The attribute's value. It's always stored as a string.
<camelCaseName>The same name in JavaScript: the data- is dropped, and each dash plus the letter after it becomes a capital letter, so data-in-stock is dataset.inStock.

Setting a name that doesn't exist yet creates the attribute. MDN's HTMLElement: dataset property page explains how the names are converted.

The Example​

The example page has a paragraph for a coffee, with a data-price and a data-in-stock attribute. The script adds a size with dataset, shows the price and stock values, and shows the paragraph's HTML afterwards.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, a page with a paragraph Coffee with data-price 4.50 and data-in-stock yes, two empty paragraphs, and a script that sets coffee.dataset.size to large, writes the price and inStock values into the first empty paragraph and the coffee paragraph's outerHTML into the second.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>dataset</title>
</head>
<body>
<p id="coffee" data-price="4.50" data-in-stock="yes">Coffee</p>
<p id="info"></p>
<p id="html"></p>

<script>
const coffee = document.getElementById('coffee');
coffee.dataset.size = 'large';

document.getElementById('info').textContent =
'Price: ' + coffee.dataset.price + ', in stock: ' + coffee.dataset.inStock;
document.getElementById('html').textContent = coffee.outerHTML;
</script>
</body>
</html>

outerHTML is the element's own HTML, written out as text, so the last line shows what the coffee paragraph looks like after the change.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium with three lines: Coffee, then Price: 4.50, in stock: yes, then the HTML of the coffee paragraph with id coffee, data-price 4.50, data-in-stock yes and data-size large.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • coffee.dataset.price read data-price, 4.50.
  • coffee.dataset.inStock read data-in-stock. The dash and the s after it became a capital S.
  • coffee.dataset.size = 'large' added data-size="large" to the paragraph, as its HTML on the third line shows.

Common Mistakes​

MistakeWhat happensDo this instead
Using the dashed name in JavaScript, dataset['in-stock']undefinedUse the camel case name: dataset.inStock
Writing a capital letter in the HTML name, data-inStockHTML attribute names are lowercase, so it became data-instock, read as dataset.instock, while dataset.inStock was undefinedUse dashes in HTML: data-in-stock
Calculating with a valuetypeof coffee.dataset.price was 'string'Convert it: Number(coffee.dataset.price)
Reading a name that isn't thereundefined, no errorCheck for undefined, or give the attribute a value in the HTML

Troubleshooting​

SymptomLikely causeFix
dataset.something is undefinedThe name doesn't match the attribute after the conversionWrite the attribute with dashes and read it in camel case
A number doesn't add upValues are stringsConvert with Number
A CSS rule such as [data-size="large"] doesn't applyThe attribute's value differs, or it was never setCheck the element's HTML in the browser's developer tools

FAQ​

How do I remove a data attribute?​

Use delete: after delete coffee.dataset.size, the paragraph no longer had a data-size attribute.

Can CSS use data attributes?​

Yes. An attribute selector such as [data-size="large"] styles elements with that attribute and value, so setting dataset.size from JavaScript can change how the element looks.

What's the difference between dataset and getAttribute?​

element.getAttribute('data-in-stock') reads the same attribute by its full HTML name. dataset is shorter, and covers only data-* attributes.

Conclusion​

data-* attributes keep small pieces of information on the element they belong to, and dataset reads and writes them in camel case. Their values are strings, and setting a new name adds the attribute to the HTML.

Additional Resources​