Skip to content

Using Data Attributes

View on Twitter

💡 Use the data-* naming convention to pass custom data to HTML elements

Then in JavaScript you can access the data through the `dataset` property

A div element inside an HTML page with two custom data attributes: data-country and data-city. We grab the element in JavaScript and access the custom data through the dataset property: document.querySelector('#card').dataset.
You might also like
Quickly Test Glob Patterns Read tip
Colour Contrast for Accessibility Read tip
Prune Docker Objects and Free up Space Read tip