How can I use an HTML attribute value as a JQuery object with $.param?
<div data-sd="{a:1,b:1,c:2}"></div>
When I try this I got strange results.
<script type="text/javascript">
var v = $.param($('div').data('sd'));
</script>
Wrap your object's property names with double-quotes, in order to have well-formed JSON:
<div data-sd='{"a":1,"b":1,"c":2}'></div>
Then, $.param will return a=1&b=1&c=2, as expected. DEMO.
From HTML5 data-* Attributes:
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.
data-sdis not a well-formed JSON object,$.paramwill actually try to convert the string"{a:1,b:1,c:2}", yielding0=%7B&1=a&2=%3A&3=1&4=%2C&5=b&6=%3A&7=1&8=%2C&9=c&10=%3A&11=2&12=%7Dinstead ofa=1&b=1&c=2.