javascript for loop function -


i have index.html

<html> <head>       <script type="text/javascript" src="scripts.js" > </script>   </head> <body> <center><h1>images</h1></center> <center>      <img id="b1" onclick="image()" src="1.jpg">     <img id="b2" onclick="image()" src="2.jpg">     <img id="b3" onclick="image()" src="3.jpg">         <img id="b4" onclick="image()" src="4.jpg">     </center>   </body> </html> 

and scripts.js

function image(){      for(var = 1; < document.images.length; i++)     {             document.write(i);     } 

when click first image show number 1 on screen , browser stuck thinking.

why doesnt show 123?

the reason why loop isn't working because first time call

document.write 

it's removing of images page, , thus, reducing length of document.images node list zero.

if want see 1234, instead of using document.write (which deprecated anyway dai pointed out) use:

document.body.innerhtml += i; 

also, dai correctly points out, first array index 0 not 1, loop should follows:

for(var = 0; < document.images.length; i++) {         document.body.innerhtml += i; } 

notice: var = 0;


Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -