javascript - using innerHTML in a loop is not properly displaying the set of json results -
i'm using html onclick event call function read javascript string containing json syntax , output results, last result being shown.
var resorts = '{ "skiresorts" : [' + '{ "resortname":"afton alps" , "resortstate":"minnesota" },' + '{ "resortname":"alpine way" , "resortstate":"pennsylvania" },' + '{ "resortname":"alyeska" , "resortstate":"alaska" } ]}'; var obj = json.parse(resorts); function displayskiresorts() { //code (i = 0; < obj.length; i++) { document.getelementbyid("req8").innerhtml = obj.skiresorts[i].resortname + " " + obj.skiresorts[i].resortstate; } }
why doesn't show entire set of ski resorts?
i changed right acces array length obj.skiresorts.length
, change assignment +=
, add line break <br>
.
var resorts = '{ "skiresorts" : [' + '{ "resortname":"afton alps" , "resortstate":"minnesota" },' + '{ "resortname":"alpine way" , "resortstate":"pennsylvania" },' + '{ "resortname":"alyeska" , "resortstate":"alaska" } ]}'; var obj = json.parse(resorts); function displayskiresorts() { (i = 0; < obj.skiresorts.length; i++) { // obj.skiresorts.length right array document.getelementbyid("req8").innerhtml += obj.skiresorts[i].resortname + " " + obj.skiresorts[i].resortstate + '<br>'; // += , <br> } } displayskiresorts();
<div id="req8"></div>
Comments
Post a Comment