jquery - Can't parse JSON to Javascript object -
i have html box:
<span>select depatament</span><span> <select id="department" onchange="enableslaveselectbox(this)" data-slaveelaments='{"a": 1, "b": "2"}'> <option selected disabled>-select-</option> </select> </span>
event onchange() implementation:
function enableslaveselectbox(element) { var d = $('#department').data('slaveelaments'); alert($.parsejson(d)); }
but when onchange() event fired on row:
alert($.parsejson(d));
this error:
syntaxerror: json.parse: unexpected character @ line 1 column 2 of json data return json.parse( data + "" );
any idea why error above? thank in advance.
in case don't need use parsejson
, because d
object
,
function enableslaveselectbox(element) { var d = $('#department').data('slaveelaments'); console.log(d.a); console.log(d.b); }
when data attribute object (starts '{') or array (starts '[') jquery.parsejson used parse string; must follow valid json syntax including quoted property names. if value isn't parseable javascript value, left string.
Comments
Post a Comment