html - javascript for loop increment by > 1 problems -
i have loop works , i+1, i+2 onwards not work. error is:
cannot read property 'logo_sm' of undefined
here's code:
var jsonobj = json.parse(http_request.responsetext); var rows = ''; for(var i=0;i<jsonobj.length;i=i+2){ rows += '<tr><td class="logo_sm">' + "<img src='"+jsonobj[i].logo_sm+"'/>" + '</td><td class="logo_sm">' + "<img src='"+jsonobj[i+1].logo_sm+"'/>" + '</td><td class="logo_sm">' + "<img src='"+jsonobj[**i+2**].logo_sm+"'/>" + '</td></tr>'; i++; } document.getelementsbytagname('table')[0].innerhtml += rows;
in loop, if increment i++ , final column has i+1 instead of i+2 works.
seems simple still in learning phase. so, struggling it.
the way loop setup trying access array indexes items not exist.
if initialize i=json.length , have loop increment i+2 each time. when equal length of json object, loop trying access elements @ length of json object plus 2, not exist.
if trying output image in each table row following:
var jsonobj = json.parse(http_request.responsetext); var rows = ''; for(var i=0;i<jsonobj.length;i++) { rows += "<tr><td class='logo_sm'><img src='"+jsonobj[i].logo_sm+"'/></td></tr>"; } document.getelementsbytagname('table')[0].innerhtml += rows;
Comments
Post a Comment