Categories
HTML

How to store and retrieve JavaScript arrays into and from HTML5 data attributes?

Spread the love

Sometimes, we want to store and retrieve JavaScript arrays into and from HTML5 data attributes.

In this article, we’ll look at how to store and retrieve JavaScript arrays into and from HTML5 data attributes.

How to store and retrieve JavaScript arrays into and from HTML5 data attributes?

To store and retrieve JavaScript arrays into and from HTML5 data attributes, we can put the array directly in the element.

For instance, we write

<div id="demo" data-stuff='["some", "string", "here"]'></div>

to set the data-stuff attribute to the ["some", "string", "here"] array string.

Conclusion

To store and retrieve JavaScript arrays into and from HTML5 data attributes, we can put the array directly in 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 *