Its been a while that I’m trying to change the src of my image using JavaScript addEventListener
. But it isn’t changing the src. The following example to explain my situation:
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");
}
For the record in my mini project, I’m not using any framework and this is very simple and easy code so there is no bug.
the html code is :
<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>
3
Answers
you need to use change event and the select elements values
You can also use a JS object: