json - jquery not parsing data held in data attribute -
i'm trying parse data held in data attribute defined follows
html
<div data-details="{'value':'2.38', 'image':tesco.png }"></div>
jquery
var details = $(this).data('details');
when trying parse json
$.parsejson(details)
it returns in console
syntaxerror: json.parse: unexpected character @ line 1 column 1 of json data
any guidance apreciated.
*edit when html declared (all variables wrapped in single quotes)
<div class="transaction-panel matched" data-details="{'value':'2.38', 'image':'tesco.png' }" ></div>
the same error reurned
your details
not valid json, $.parsejson
can't read it. need use double quotes around keys , (string) values. valid json needs double quotes.
<div data-details='{"value":2.38, "image":"tesco.png"}'></div>
note: can use either single or double quotes html attributes.
as noted @vohuman, when $(this).data('details')
, jquery automagically parse json (it detect it's json, parse it), don't need $.parsejson
.
here's demo @swatkins made: http://jsfiddle.net/dpqpl9kv/
Comments
Post a Comment