开发者

javascript while loop and inner html

开发者 https://www.devze.com 2023-02-22 19:13 出处:网络
I can\'t figure this one out. Why is this breaking down? My alert statements show me clearly that the elements are all coming in but if i replace them with the inner html line it stops after the first

I can't figure this one out. Why is this breaking down? My alert statements show me clearly that the elements are all coming in but if i replace them with the inner html line it stops after the first iteration.

while(i<6)
        {
        document.getElementById("cell-"+i).innerHTML  = out[i] ;
        i++ ;
        }

below variation shows me that all elements come in and the cell names are correctly constructed:

while(i<6)
        {
        alert("cell-"+i)
        alert(out[i])
        i++ ;
        }

[EDIT]

ok judging from your feedback it's not a syntax or loop construction issue which i hoped could be easily fixed. Here's more background:

I'm using this to populate a table with parsed data from an Ajax call. The data format is JSON. It's a little search engine for photos. IF my first search is for keywords that can be found the table is populated. If HOWEVER i first search for some gibberish (e.g.: dfadfasfad), no results are retrieved (so far so good). If i THEN search for a word that would retrieve results, the whole thing breaks down. So IT IS a Ajax problem after all? Sorry, the code is way too long to post here i think. If my problem rings a bell let me know. Else no worries.

[EDIT2]

from Firebug:

document.getElementById(
display(out=["Colorful Drinks by the ...a0c1a.jpg"><br /><br />", "Peru-100526-473<br /><i...f7908.jpg"><br /><br />", "AR6E0549<br /><img src=...8bfea.jpg"><br /><br />", 3 more...])AjaxUpdater.js (line 128)
onResponse()AjaxUpdater.js (line 65)
[Break On This Error] document.getElementById("cell-"+i).innerHTML = out[i] ; 

[EDIT3]

开发者_如何学C

ok i'm getting closer. it seems that the innerHTML assignments modify the page structure

BEFORE

javascript while loop and inner html

AFTER

javascript while loop and inner html


It works the first time but not if you get an error and try again?

But it is breaking on this line:

document.getElementById("cell-"+i).innerHTML = out[i]

I'm going to guess that document.getElementById("cell-"+i) is returning null, so calling innerHTML on it is giving the error. You could split it up into two lines to verify this.

What I think is going wrong is that when you display the error you remove the cell-# items to display the "No Results Found" message. Then on another search, you never put them back in.

Could you add the html code for what the table looks like on the success and what it looks like on a fail, before you try again? Perhaps that would shed some light on the subject.


My guess would be that the id you're looking for simply isn't valid. You'd need to post both the code that initializes i (we don't know what value it starts at) and your html so that we can see if an element exists with that id.


Not sure exactly what "the whole thing breaks down" means. Have you tried debugging with firebug? Perhaps the Ajax response is returning less than items than you are expecting? while(i<6) Your question source code is edited making it hard to know what you've omitted.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号