startingtocode
startingtocode

Reputation: 3

Selecting a dropdown choice and having different images appear (using Javascript)

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>

Upvotes: 0

Views: 180

Answers (2)

MagJS
MagJS

Reputation: 422

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!

Upvotes: 1

guest271314
guest271314

Reputation: 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>

Upvotes: 1

Related Questions