How to set JSON format to HTML5 data attributes in the jQuery

In order to be parsed as an object, the data attribute must be a well formed JSON object.

In your case you just need to quote the object keys (as you do in the second object). Try:

<div data-params="{"a": 1, "b": "2"}" id="TEST1"></div>

For more info see the data method docs, the relevant part is this one (emphasis mine):

Every attempt is made to convert the string to a JavaScript value
(this includes booleans, numbers, objects, arrays, and null) otherwise
it is left as a string… …When the
data attribute is an object (starts with '{') or array (starts with
'[') then jQuery.parseJSON is used to parse the string; it must follow
valid JSON syntax including quoted property names.

Leave a Comment

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