0

I am trying to have different images come up after selecting an option from a dropdown. I know it's probably very basic, but I am very new to HTML and Javascript. Not sure where or why the code isn't producing the correct output. Thanks!

<script type = "text/javascript">
    function displayImage(elem) {
    var img = document.getElementById("dessert");
    image.src = elem.value;
    }
    </script>

<form name="controls">
<select name="dessertchoice" onchange="displayImage(this);">
    <option value="">None</option>
    <option value="http://web.ics.purdue.edu/~lrourk/IE332/chocolate.jpg">Chocolate</option>
    <option value="http://web.ics.purdue.edu/~lrourk/IE332/icecream.jpg">Ice Cream</option>
    <option value="http://web.ics.purdue.edu/~lrourk/IE332/fruit.jpg">Fruit</option>
    <option value="http://web.ics.purdue.edu/~lrourk/IE332/brule.jpg">Creme Brulee</option> 
</select>
</br></br>

<td>
<img id="dessert" src="" style="width:300px;height:200px;"/> 
</td></tr>
</form>

2 Answers 2

1

it's just a simple typo, happens to the best of us :)

Change this image.src = elem.value; to img.src = elem.value;

Here is the working example: http://jsbin.com/jamixipiza/1/edit?html,output

Hope that helps!

Sign up to request clarification or add additional context in comments.

Comments

1

Try substituting img for image within displayImage

<script type = "text/javascript">
    function displayImage(elem) {
    var img = document.getElementById("dessert");
    img.src = elem.value;
    }
    </script>

<form name="controls">
<select name="dessertchoice" onchange="displayImage(this);">
    <option value="">None</option>
    <option value="http://web.ics.purdue.edu/~lrourk/IE332/chocolate.jpg">Chocolate</option>
    <option value="http://web.ics.purdue.edu/~lrourk/IE332/icecream.jpg">Ice Cream</option>
    <option value="http://web.ics.purdue.edu/~lrourk/IE332/fruit.jpg">Fruit</option>
    <option value="http://web.ics.purdue.edu/~lrourk/IE332/brule.jpg">Creme Brulee</option> 
</select>
</br></br>

<td>
<img id="dessert" src="" style="width:300px;height:200px;"/> 
</td></tr>
</form>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.