Javascript - onchange image change - Stack Overflow

This should be fairly simple but it's not working and I'm not seeing it.I'm trying to

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
Add a ment  | 

3 Answers 3

Reset to default 1

If 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

相关推荐

  • Javascript - onchange image change - Stack Overflow

    This should be fairly simple but it's not working and I'm not seeing it.I'm trying to

    7小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信