How to Read and Set data-* Attributes with dataset in JavaScript
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
<element data-<name>="<value>">
<element>.dataset.<camelCaseName>
<element>.dataset.<camelCaseName> = <value>;
| Part | What 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:

<!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 screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
coffee.dataset.pricereaddata-price,4.50.coffee.dataset.inStockreaddata-in-stock. The dash and thesafter it became a capitalS.coffee.dataset.size = 'large'addeddata-size="large"to the paragraph, as its HTML on the third line shows.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Using the dashed name in JavaScript, dataset['in-stock'] | undefined | Use the camel case name: dataset.inStock |
Writing a capital letter in the HTML name, data-inStock | HTML attribute names are lowercase, so it became data-instock, read as dataset.instock, while dataset.inStock was undefined | Use dashes in HTML: data-in-stock |
| Calculating with a value | typeof coffee.dataset.price was 'string' | Convert it: Number(coffee.dataset.price) |
| Reading a name that isn't there | undefined, no error | Check for undefined, or give the attribute a value in the HTML |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
dataset.something is undefined | The name doesn't match the attribute after the conversion | Write the attribute with dashes and read it in camel case |
| A number doesn't add up | Values are strings | Convert with Number |
A CSS rule such as [data-size="large"] doesn't apply | The attribute's value differs, or it was never set | Check 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
- HTMLElement: dataset property, on MDN.
- data-* global attributes, on MDN.
- Use data attributes, on MDN.
