admin管理员组文章数量:1392088
Trying to read JSON from hidden input value.
<html>
<body>
<input id="hdn" type="hidden" value='{"products":{"id":100001,name:"Ram"}}'>
<script type="text/javascript">
var jsonObj = document.getElementById('hdn').value;
alert(jsonObj);
alert(jsonObj.products.name);
</script>
</body>
</html>
Trying to read JSON from hidden input value.
<html>
<body>
<input id="hdn" type="hidden" value='{"products":{"id":100001,name:"Ram"}}'>
<script type="text/javascript">
var jsonObj = document.getElementById('hdn').value;
alert(jsonObj);
alert(jsonObj.products.name);
</script>
</body>
</html>
Share
Improve this question
edited Aug 24, 2016 at 10:21
Lokesh Yadav
asked Feb 18, 2012 at 7:42
Lokesh YadavLokesh Yadav
1,6025 gold badges25 silver badges50 bronze badges
1 Answer
Reset to default 6You need to parse it as var jsonObj = JSON.parse(document.getElementById('hdn').value)
Note, I changed the way you were storing your JSON object, by adding the quotes to the name
property. I added as both console.log and an alert... mostly because I prefer console.log, but you originally had an alert in there.
Here's the updated (working) code:
<html>
<body>
<input id="hdn" type="hidden" value='{"products":{"id":100001,"name":"Ram"}}'>
<script type="text/javascript">
var jsonObj = JSON.parse(document.getElementById('hdn').value);
console.log(jsonObj);
console.log(jsonObj.products.name);
alert(jsonObj);
alert(jsonObj.products.name);
</script>
</body>
</html>
本文标签: javascriptReading json from hidden input valueStack Overflow
版权声明:本文标题:javascript - Reading json from hidden input value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744780708a2624693.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论