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
Colour Contrast for Accessibility Read tip
Debug Requests Using a Mirror API Read tip
Update Packages on Ubuntu Read tip