admin管理员组文章数量:1355625
I am going to have 5 images and 5 buttons, currently only the single button. On button click I would like to show only the image that is associated with the button. for that I would like to write single function that gets the image path as parameter. I am very new to JavaScript, Bellow is a code I found and edited a bit:
<script>
function pictureChange(path) {
console.log(path);
document.getElementById("theImage").src=path;
}
</script>
<body>
<img id="theImage" src=".png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange(".png")"></p>
</body>
I am going to have 5 images and 5 buttons, currently only the single button. On button click I would like to show only the image that is associated with the button. for that I would like to write single function that gets the image path as parameter. I am very new to JavaScript, Bellow is a code I found and edited a bit:
<script>
function pictureChange(path) {
console.log(path);
document.getElementById("theImage").src=path;
}
</script>
<body>
<img id="theImage" src="http://31.media.tumblr./18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange("http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png")"></p>
</body>
Share
Improve this question
edited Aug 21, 2017 at 9:27
Matthew Fitch
2651 gold badge9 silver badges13 bronze badges
asked Aug 21, 2017 at 8:57
Tamir EinyTamir Einy
1861 silver badge13 bronze badges
4 Answers
Reset to default 6Currently issue in you code is, when you passing string in function you need to the sequence of inverted ma's. for example while using "", you can use only '' single inverted ma'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./18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"></p>
</body>
for showing 5 images, you can use single button too. As per you requirement, you can create array of images sources and pass index on button click, as well as you can do the same which you were doing in your current snippet i.e passing source on button click.
Also, for single button what you can do is pass image src and find the index of that src index in array, from that index you change to next index and assign source to your image element. Please make sure to check if you are in last index than src at 0 index will get assigned, otherwise you will get stuck in error.
updated snippet: css display: inlne-block property will help you. also you need to change the width of image, because by default it
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./18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"></p>
</body>
Now it's working, your "" were confusing the browser cause you can't nest with the same char, you must alternate " and ' when you nest
function pictureChange(path) {
console.log(path);
document.getElementById("theImage").src=path;
}
<img id="theImage" src="http://31.media.tumblr./18b5f8f0a00ad01e50f7ae2f513be52d/tumblr_msqcl4iwM01soh1p8o1_500.png">
<p><input type="button" id="theButton" value="click me!" onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"></p>
U wrong on use symbols "
.
Inside onclick
function pictureChange
u had symbol "
then html throw syntax error.
u need use symbol '
changing for "
onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"
Good luck!
Current error so far with your code
onclick="pictureChange('http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"
You got confused with quote. Wrap with single quote if your outer quotes are double quotes.
currently only the single button. On button click I would like to show only the image that is associated with the button.
Just to make it more generic, pass the image id aswell.
function pictureChange(imgid,path) {
console.log(path);
document.getElementById(imgid).src=path;
}
And you can use it as
onclick="pictureChange('theImage','http://31.media.tumblr./fca646cd8fe87906e605ad7e8d039903/tumblr_mmoz4fWT6U1soh1p8o1_500.png')"
本文标签: javascriptChange picture in HTML using multiple buttonsStack Overflow
版权声明:本文标题:javascript - Change picture in HTML using multiple buttons - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744043446a2581088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论