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
Relinquish Process Privileges in Node.js Read tip
Make Your Site Portable By Using Permalinks Read tip
setTimeout Execution Time Read tip