store and retrieve javascript arrays into and from HTML5 data attributes

It turned out that you could use the html escaped characters in the element data attribute to have json-like array (encoded are quotes):

<div id="demo" data-stuff="[&#34;some&#34;, &#34;string&#34;, &#34;here&#34;]"></div>

And then in javascript get it without any additional magic:

var ar = $('#demo').data('stuff');

Check this fiddle out.

Edited (2017)
You don’t need to use html escaped characters in the data attribute.

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

Check this new fiddle out.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)