Categories
JavaScript Answers

How to get list of data-* attributes using JavaScript?

Spread the love

Sometimes, we want to get list of data-* attributes using JavaScript.

In this article, we’ll look at how to get list of data-* attributes using JavaScript.

How to get list of data-* attributes using JavaScript?

To get list of data-* attributes using JavaScript, we use the element’s dataset property.

For instance, we write

const attributes = element.dataset;

to use the element.dataset object to get the data- attributes of element.

The property keys would be the name after the hyphen.

And the values would be the attribute values.

Conclusion

To get list of data-* attributes using JavaScript, we use the element’s dataset property.

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 *