Interactive online courses HTML Academy
2026-03-09 10:57 Diff

We learned how to work with elements in a collection and made the pop-up appear when you click on any of the buttons. Text should be displayed inside the pop-up that provides an explanation of the word that the user clicked on. The tooltip text is stored in the markup in the attribute data-tooltip-text of the button itself.

What is this attribute? HTML is a flexible language, and you can create your own attributes in it. The names of these attributes start with the data- prefix followed by any word that the developer is free to choose. This is similar to variables: you decide how to name the attribute and what value it should have.

For example:

<div data-cat-name="Muffin">

Attributes that start with data- are usually used to store supporting information. You can read more about them here.

How do you obtain the value of such an attribute in JavaScript? To do this, use the dataset property followed by the attribute name without the data- prefix:

element.dataset.attributeNameWithoutPrefix

If the attribute name consists of several words that use hyphens, then in JavaScript it is written in camelCase: the hyphens are removed, and each word is capitalized except for the first one. To get the attribute value from the example above, use the following instructions:

let element = document.querySelector('div'); console.log(element.dataset.catName); // Displays: Muffin

On our site, the tooltip texts are stored in the data-tooltip-text attributes. Let’s see how they look in the markup, and then output the value of this attribute for each button to the console.

You can create custom attributes without the data- prefix, but this code will not comply with the standard or specification. If you were to do this, then we say that the code is invalid. You can check the validity of the code using the validator.