I am currently working on implementing an IF ELSE statement in JavaScript to change the source image of a picture when a button is clicked. The idea is that when a user clicks on a specific body type button for a car, it will update the background image within three existing buttons. Upon clicking one of those buttons, the image source should change accordingly.
Unfortunately, every time I test the code, it only displays the picture associated with the first IF statement. I could really use some assistance with this issue, especially since I am still in the process of teaching myself JavaScript.
HTML
<div id="vehicleModelFrame">
<button id="modelFrameButton1" type="button"></button>
<button id="modelFrameButton2" type="button"></button>
<button id="modelFrameButton3" type="button"></button>
</div>
<div id="selectTrimTitle">
<h3>
Vehicle Trim
</h3>
</div>
<div id="vehicleTrimFrame">
<div class="trimSelector">
<ul class="nav nav-tabs">
<li class="active"><a data-toggle="tab" href="#home">Trim 1</a></li>
<li><a data-toggle="tab" href="#menu1">Trim 2</a></li>
<li><a data-toggle="tab" href="#menu2">Trim 3</a></li>
<li><a data-toggle="tab" href="#menu3">Trim 4</a></li>
</ul>
<div class="tab-content">
<div id="home" class="tab-pane active">
<img id="carLogo1" src="" alt="carLogo">
</div>
<div id="menu1" class="tab-pane">
<img id="carLogo2" src="noimage.png" alt="carLogo">
</div>
<div id="menu2" class="tab-pane">
<img id="carLogo3" src="noimage.png" alt="carLogo">
</div>
<div id="menu3" class="tab-pane">
<img id="carLogo4" src="noimage.png" alt="carLogo">
</div>
</div>
</div>
</div>
JAVASCRIPT
document.getElementById("modelFrameButton1").addEventListener("click", function showTrim1(){
var carImg = document.getElementById("carLogo1");
var backImg = document.getElementById("modelFrameButton1");
if (document.getElementById("modelFrameButton1").style.backgroundImage = "JeepWrangler.png") {
carImg.src = "Jeep_Wrangler.png";
}else if (document.getElementById("modelFrameButton1").style.backgroundImage = "BMW3.png") {
carImg.src = "BMW3.png";
}
In order to better understand JavaScript, I am looking for solutions that are based on JavaScript principles.