admin管理员组文章数量:1418052
This should be fairly simple but it's not working and I'm not seeing it. I'm trying to get my image to change with the onchange method calling the displayImage function. Any ideas what I'm missing?
<html>
<head>
<title>Select Image</title>
<script type="text/javascript">
function displayImage() {
var image = document.getElementById("canvas");
var newImage = image.option[image.selectedIndex].value;
}
</script>
</head>
<body>
<form name="controls">
<img id="canvas" src="pictures/fire1.jpg" />
<select name="imageList" onchange="displayImage();">
<option value="pictures/fire1.jpg">Fire 1</option>
<option value="pictures/fire2.jpg">Fire 2</option>
<option value="pictures/fire3.jpg">Fire 3</option>
<option value="pictures/fire4.jpg">Fire 4</option>
</select>
</form>
</body>
</html>
This should be fairly simple but it's not working and I'm not seeing it. I'm trying to get my image to change with the onchange method calling the displayImage function. Any ideas what I'm missing?
<html>
<head>
<title>Select Image</title>
<script type="text/javascript">
function displayImage() {
var image = document.getElementById("canvas");
var newImage = image.option[image.selectedIndex].value;
}
</script>
</head>
<body>
<form name="controls">
<img id="canvas" src="pictures/fire1.jpg" />
<select name="imageList" onchange="displayImage();">
<option value="pictures/fire1.jpg">Fire 1</option>
<option value="pictures/fire2.jpg">Fire 2</option>
<option value="pictures/fire3.jpg">Fire 3</option>
<option value="pictures/fire4.jpg">Fire 4</option>
</select>
</form>
</body>
</html>
Share
Improve this question
asked Sep 20, 2014 at 13:46
HeatherHeather
251 gold badge3 silver badges7 bronze badges
1
- Is the function being called at all? For example, if you put an alert inside displayImage(), would the alert be called? This way, you would know if it's a problem of function-not-being-called-at-all vs. function-is-called-but-not-doing-what-you-want. – The One and Only ChemistryBlob Commented Sep 20, 2014 at 13:54
3 Answers
Reset to default 1If you want to change the image of the canvas element based on value of the dropdown list, use something like:
<html>
<head>
<title>Select Image</title>
<script type="text/javascript">
function displayImage(elem) {
var image = document.getElementById("canvas");
image.src = elem.value;
}
</script>
</head>
<body>
<form name="controls">
<img id="canvas" src="pictures/fire1.jpg" />
<select name="imageList" onchange="displayImage(this);">
<option value="pictures/fire1.jpg">Fire 1</option>
<option value="pictures/fire2.jpg">Fire 2</option>
<option value="pictures/fire3.jpg">Fire 3</option>
<option value="pictures/fire4.jpg">Fire 4</option>
</select>
</form>
</body>
</html>
Add this
to the function call and modify the function itself.
Try this:
function displayImage() {
var image = document.getElementById("canvas"),
select = document.getElementsByTagName('select')[0];
image.src = select.value;
}
or more pact:
function displayImage() {
document.getElementById("canvas").src = document.getElementsByTagName('select')[0].value;
}
Here is the example
you can try this:
change your select
name to id
.
<html>
<head>
<title>Select Image</title>
<script type="text/javascript">
function displayImage() {
var image = document.getElementById("imageList").value;
document.getElementsByTagName("img")[0].setAttribute("src",image)
}
</script>
</head>
<body>
<form name="controls">
<img id="canvas" src="pictures/fire1.jpg" />
<select id="imageList" onchange="displayImage();">
<option value="pictures/fire1.jpg">Fire 1</option>
<option value="pictures/fire2.jpg">Fire 2</option>
<option value="pictures/fire3.jpg">Fire 3</option>
<option value="pictures/fire4.jpg">Fire 4</option>
</select>
</form>
</body>
</html>
本文标签: Javascriptonchange image changeStack Overflow
版权声明:本文标题:Javascript - onchange image change - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745248415a2649671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论