javascript - Change picture in HTML using multiple buttons -


i going have 5 images , 5 buttons, single button. on button click show image associated button. write single function gets image path parameter. new javascript, bellow code found , edited bit:

<script>     function picturechange(path) {         console.log(path);         document.getelementbyid("theimage").src=path;     } </script> <body>     <img id="theimage" src="http://31.media.tumblr.com/18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwm01soh1p8o1_500.png">     <p><input type="button" id="thebutton" value="click me!" onclick="picturechange("http://31.media.tumblr.com/fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fwt6u1soh1p8o1_500.png")"></p> </body> 

currently issue in code is, when passing string in function need sequence of inverted comma's. example while using "", can use '' single inverted comma's inside of it.

<script>  function picturechange(path) {  console.log(path);  document.getelementbyid("theimage").src=path;  }  </script>  <body>  <img id="theimage" src="http://31.media.tumblr.com/18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwm01soh1p8o1_500.png">  <p><input type="button" id="thebutton" value="click me!" onclick="picturechange('http://31.media.tumblr.com/fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fwt6u1soh1p8o1_500.png')"></p>  </body>

for showing 5 images, can use single button too. per requirement, you can create array of images sources , pass index on button click, can same doing in current snippet i.e passing source on button click.

also, single button can pass image src , find index of src index in array, index change next index , assign source image element. please make sure check if in last index src @ 0 index assigned, otherwise stuck in error.

updated snippet: css display: inlne-block property you. need change width of image, because default

img {    width:85%;  }  p {    display: inline-block;  }
<script>  function picturechange(path) {  console.log(path);  document.getelementbyid("theimage").src=path;  }  </script>  <body>  <img id="theimage" src="http://31.media.tumblr.com/18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwm01soh1p8o1_500.png">  <p><input type="button" id="thebutton" value="click me!" onclick="picturechange('http://31.media.tumblr.com/fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fwt6u1soh1p8o1_500.png')"></p>  </body>


Comments

Popular posts from this blog

python - Operations inside variables -

Generic Map Parameter java -

arrays - What causes a java.lang.ArrayIndexOutOfBoundsException and how do I prevent it? -