Reputation: 1978
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>
Upvotes: 1
Views: 374
Reputation: 91299
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.
Upvotes: 4