javascript - Having Trouble Switching Images -
i'm learning java. learning if, else statements.
in game creating, user picks number between 0 , 10 , puts input box. if correct, image on screen changes 1 picture, if incorrect, switches different picture. however, cannot seem images change @ all. i've tried few different ways of coding; i'm using img array. however, when code receive objecthtmlimageelement error.
here current code:
<div id="top"> <h1>pie in face</h1> <p>guess how many fingers i'm holding between 0 , 10. <br /> if guess correctly, pie in face. <br /> if guess wrong, pie in face.</p> <input id="answer" /> <button id="mybutton">submit</button> </center> </div> <div id="container"> <div id="image"></div> <div id="manpie"></div> <div id="girlpie"></div> </div> <script type="text/javascript"> var x = math.random(); x = 11 * x; x = math.floor(x); var imgarray = new array(); imgarray[0] = new image(); imgarray[0].src = "images/manpie2.jpg"; imgarray[1] = new image(); imgarray[1].src = "images/girlpie2.jpg"; document.getelementbyid("mybutton").onclick = function() { if (x == document.getelementbyid("answer").value) { document.getelementbyid("image").innerhtml = imgarray[0]; // had document.getelementbyid("image").innerhtml=imgarray[0]; } else { document.getelementbyid("image").innerhtml = imgarray[1]; } } </script> </body>
i have tried using lines such as: document.getelementbyid("image").innerhtml=document.getelementbyid("manpie");
and nothing works. here link "live" site it's on. http://176.32.230.6/mejorarcr.com/
any appreciated. thank you!
you have change innerhtml
value in code:
if (x==document.getelementbyid("answer").value) { document.getelementbyid("image").innerhtml='<img src="'+imgarray[0].src+'" />'; // had document.getelementbyid("image").innerhtml=imgarray[0].src; } else { document.getelementbyid("image").innerhtml='<img src="'+imgarray[1].src+'" />'; }
Comments
Post a Comment