Skip to main content
2 of 2
added 70 characters in body
Pablo Fernandez
  • 105.6k
  • 59
  • 196
  • 234

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.

Pablo Fernandez
  • 105.6k
  • 59
  • 196
  • 234