javascript - Push the button and change randomly the image -


i'm new in site, follow many years.

in test i'm trying change image pressing button. image generate randomly. in short, have 2 dice , 1 button change them. here, code.

html:

<tr align="center"> <td width="20%">  <input type="button" name="tira" value="tira" onclick="lancio()">  <td><img id="scelta1" alt="alt1" width="30px"></img>  <td id="scelta2"> <img src="images/dado2.gif" alt="2" width="30px"></img> 

here javascript.

<script> var scelta1; var temp=0;  var image = new array(6); image[0]= 'images/dado1.gif'; image[1]= 'images/dado2.gif'; image[2]= 'images/dado3.gif'; image[3]= 'images/dado4.gif'; image[4]= 'images/dado5.gif'; image[5]= 'images/dado6.gif';  function lancio(){     temp = math.floor((math.random() * 6));          if(temp == 0){             document.getelementbyid("scelta1").src = image[0];             document.getelementbyid("alt1").value = temp;             return scelta1;         }         else if(temp == 1){             document.getelementbyid("scelta1").src = image[1];             document.getelementbyid("alt1").value = temp;             return scelta1;         }         else if(temp == 2){             document.getelementbyid("scelta1").src = image[2];             document.getelementbyid("alt1").value = temp;             return scelta1;         }         else if(temp == 3){             document.getelementbyid("scelta1").src = image[3];             document.getelementbyid("alt1").value = temp;             return scelta1;         }         else if(temp == 4){             document.getelementbyid("scelta1").src = image[4];             document.getelementbyid("alt1").value = temp;             return scelta1;         }         else if(temp == 5){             document.getelementbyid("scelta1").src = image[5];             document.getelementbyid("alt1").value = temp;             return scelta1;         }      } }  </script> 

thank support , hope in many answers. sorry if wrong forum's rules.

bye

first there no such thing <img></img>. self closing tag.

<img src="http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg" /> <button onclick=lancio();>yoo</button> 

here's code work you:

<img src="http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg" id="image" />    function lancio() {    var temp=0;    var image = new array(6); image[0]= 'http://www.joomlaworks.net/images/demos/galleries/abstract/7.jpg'; image[1]= 'http://images.visitcanberra.com.au/images/canberra_hero_image.jpg'; image[2]= 'http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg'; image[3]= 'http://www.jpl.nasa.gov/spaceimages/images/mediumsize/pia17011_ip.jpg'; image[4]= 'http://static.guim.co.uk/sys-images/guardian/pix/pictures/2014/4/11/1397210130748/spring-lamb.-image-shot-2-011.jpg'; image[5]= 'http://www.keenthemes.com/preview/metronic/theme/assets/global/plugins/jcrop/demos/demo_files/image1.jpg';    temp = math.floor(math.random() * 6);    document.getelementbyid("image").src=image[temp];   } 

Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -