I have been attempting to change the src of an image using JavaScript's addEventListener, but for some reason it is not working. Here is an example to illustrate my issue:
let bulbImage = $.querySelector(".bulb-image");
let turnOnOption = $.querySelector(".bulb-on");
let trunOffOption = $.querySelector(".bulb-off");
turnOnOption.addEventListener("click" , turnOnFunction);
function turnOnFunction () {
bulbImage.setAttribute("src" , "./images/bulbon.gif");
}
trunOffOption.addEventListener("click" , turnOffFunction);
function turnOffFunction () {
bulbImage.setAttribute("src" , "./images/bulboff.gif");
}
Just to clarify, I am not using any framework in this small project and the code is quite simple and straightforward so there should be no bugs.
The HTML code is as follows:
<img src="./images/bulboff.gif" class="bulb-image" alt="bulboff">
<select class="selectbox">
<option value="Turn ON" class="bulb-on">Turn ON</option>
<option value="Turn OFF" class="bulb-off">Turn OFF</option>
</select>