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>
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745248417a4618526.html
评论列表(0条)