Wednesday, 15 September 2010

Display image through JavaScript function -



Display image through JavaScript function -

i'm writing javascript function flips coin , i'm trying homecoming image far no luck. utilize img src tags assign value variable somehow suspect not work , wanted check guys on how done?

here code:

<script type = text/javascript> var headscount = 0; var tailscount = 0; function start() { var buttontwo = document.getelementbyid("coinbutton"); buttontwo.addeventlistener("click", coinresult, false); } function flip() { var facevalue = math.floor((math.random()*2)+1); if (facevalue == 1) { headscount++; homecoming true; } else { tailscount++; homecoming false; } } function coinresult() { var result = document.getelementbyid("cointoss"); var coincount = document.getelementbyid("countnumber"); <img name = "heads" src = "images/heads.png" /> if (flip() == true) { result.innerhtml = <img src = "images/heads.png" alt = "heads" /> } else { result.innerhtml = <img src = "images/tails.png" alt = "tails" /> } coincount.innerhtml = "heads count: " + headscount + " tails count: " + tailscount; } window.addeventlistener("load", start, false); </script>

the innerhtml property must receive string represents html, so:

result.innerhtml = "<img src='images/heads.png' alt='heads' />";

if want utilize quotes instead of single quotes in html (yes can, , people prefer it), have escape quotes (by adding backslash before it) javascript engine can understand quotes text , not marking start or end of string, like:

result.innerhtml = "<img src=\"images/heads.png\" alt=\"heads\" />";

ps: i'm considering cointoss element div.

javascript

No comments:

Post a Comment