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.