Categories
JavaScript Answers

How to get the values of data attributes in JavaScript?

Spread the love

To get the values of data attributes in JavaScript, we use the dataset property of the element.

For instance, we write

const id = document.querySelector("html").dataset.pbUserId;

to select the html element with querySelector.

And then we use the dataset property to get the value of the data-pbUserId attribute of the element.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *