Okay, you're doing several things wrong.
Your function is called volvoCar
and you are attempting to use a function called VolvoCar
- JavaScript is case sensitive.
This isn't the best way to assign an event-listener. You're adding it in the HTML, which is considered 'messy' (see Unobtrusive JavaScript). Also, you want to attach the function
, not the result of the function (which you are doing by calling it). Functions are first-class objects in JavaScript.
onclick
is the wrong event handler to use in this case. You want to use the onchange
handler of the <select>
element.
So:
HTML:
<img id="image" src="Null_Image.png"/>
<select id="CarList">
<option value="Null">No Car</option>
<option value="Volvo">Volvo</option>
<option value="Audi">Audi</option>
</select>
JS:
var changeCarImage = function () {
document.getElementById('image').src = this.options[this.selectedIndex].value + "_Image.png"
}
var carList = document.getElementById('CarList');
carList.addEventListener('change', changeCarImage, false); // Note this has some issues in old browsers (IE).
This can be seen working here!