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
Github Actions: Workflows vs Actions Read tip
Debug Requests Using a Mirror API Read tip
Mapping of HTTP Status Messages in Node.js Read tip