How to store JSON objects in data attribute in HTML?

Spread the love

Sometimes, we want to store JSON objects in data attribute in HTML.

In this article, we’ll look at how to store JSON objects in data attribute in HTML.

How to store JSON objects in data attribute in HTML?

To store JSON objects in data attribute in HTML, we set the data attribute to the JSON string.

For instance, we write

<div data-foobar='{ "foo": "bar" }'></div>

to set the data-foobar attribute to { "foo": "bar" } to store it.

Conclusion

To store JSON objects in data attribute in HTML, we set the data attribute to the JSON string.

Leave a Reply

Your email address will not be published.