A member card on your page only shows a name: Sam. But the JavaScript code you write later needs to know more: the member's id, the card's colour, whether the card is active. Information like that doesn't need to show on the page. It only needs to be attached to the card's element.
That's what data-* attributes are for: attributes you create yourself to attach extra information to an HTML element. You replace the star with a name you choose, for example data-color or data-user-id. The browser doesn't show this information on the page. Your CSS and JavaScript read it and use it.
Attach information to an element
Here is a member card with three data attributes:
<div class="card" data-color="blue" data-active="true" data-user-id="42">
Sam
</div>
<div class="card" ...>is the opening tag. Every attribute goes here, after the tag name, separated by spaces. An attribute is extra information for a tag, written asname="value".data-color="blue"is a data attribute: its name isdata-colorand its value isblue. You choose the part afterdata-yourself, in lowercase letters.data-active="true"anddata-user-id="42"are two more data attributes. One element can have as many data attributes as you like. When a name has several words, such asdata-user-id, put a hyphen between the words.Samis the text that shows, written between the opening tag and the closing tag</div>.
On the page you only see the word "Sam". The values blue, true and 42 don't show, but they stay attached to the element.
You can put data attributes on any tag, including buttons. Remember that a button's text is not an attribute. It still goes between the opening and closing tags:
<button data-action="delete" data-id="7">Delete</button>
Why must the name start with data-? An attribute you invent without this prefix, such as shade="blue", is not valid (it breaks the rules of HTML). HTML could also add an official attribute with the same name one day, and the two would conflict. The data- prefix is reserved for your own information, so it is safe.
Read them from JavaScript with dataset
This is JavaScript code, and you don't need to know how to write it yet. The first line gets Sam's card element. The next two lines read its data attributes. The comment at the end of each line shows the result:
const card = document.querySelector(".card");
card.dataset.color; // "blue"
card.dataset.userId; // "42"
datasetholds all of the element's data attributes, written without thedata-prefix. Sodata-coloris read asdataset.color.- A name with hyphens changes to camelCase: the hyphen is removed and the letter after it becomes a capital.
data-user-idbecomesdataset.userId. - The value is always text (a string), even when it holds a number. Need a number? Convert it first with
Number(card.dataset.userId).
Let CSS pick elements by them
Here is one CSS rule. It gives a light blue background to every element whose data-color is blue:
[data-color="blue"] { background: lightblue; }
The square brackets mean "pick the elements that have this attribute with this value". Sam's card matches, so its background is light blue. If you change data-color="blue" in the HTML to "green", your CSS rule for green is used instead. You don't need a new class.
⚠️ Common mix-ups with data attributes
- Text written as an attribute.
<button save>makes a button with no text. The text goes between the tags:<button ...>Save</button>. - Capital letters in the name. The browser changes
data-userIdtodata-userid(all lowercase), sodataset.userIdfinds nothing. Writedata-user-id. - Secret information. Anyone can see data attributes with view-source (the browser feature that shows a page's code). Never put passwords or tokens (secret codes that prove a login) here.
- Forgetting the value is a string.
card.dataset.active === trueis always false, because the value is text, andtruewithout quotes is not text. Compare with text:=== "true".
TL;DR: A data-* attribute is an attribute you create yourself. It attaches information to an element without showing it on the page. Write it in the opening tag, with a lowercase name after data- and the value in quotes. One element can have many data attributes. JavaScript reads them through dataset with camelCase names: the hyphen is removed and the next letter becomes a capital (data-user-id becomes dataset.userId). The values are always text (strings). CSS picks elements by them with [data-color="blue"].
⚡ Information that sticks but doesn't show
Write:
<div class="card" data-color="blue" data-active="true" data-user-id="42">
Sam
</div>
Meaning: An attribute that starts with data- is called a data attribute. The page only shows "Sam". The values blue, true and 42 don't show, but they stay attached to the element, for CSS and JavaScript to read. You choose the name after data- yourself: lowercase, with hyphens between words.
⚡ Why the data- prefix
Meaning: An attribute you invent without the prefix, such as shade="blue", isn't valid (it breaks the rules of HTML). HTML could also add an official attribute with the same name one day, and the two would conflict. The data- prefix is reserved for your own information, so it is safe.
⚡ Read by JavaScript through dataset
Write:
const card = document.querySelector(".card");
card.dataset.color; // "blue"
card.dataset.userId; // "42"
Guess first: is card.dataset.userId the number 42?
No, it's the text "42". A data attribute's value is always text (a string). Need a number? Convert it first with Number().
Meaning: The first line gets the card element. In dataset, each attribute's name is written without the data- prefix. A name with hyphens changes to camelCase (the hyphen is removed, the next letter becomes a capital): data-user-id becomes dataset.userId.