I'm facing an issue with my code. The scenario is, upon clicking the first door, an image opens based on a random number condition; for example, 1 equals an empty door and 2 equals finding a person behind it.
Now, when I click on the second or third door, the previously opened door should revert to its original image, but currently, only the first door is functioning as expected.
To address this problem, I believe adding ".is-selected" might be necessary, but I am unsure about its implementation.
Instead of utilizing HTML, I aim to accomplish this using JavaScript.
const doorTarget = document.querySelector(".door-track");
const doorClick = Array.from(doorTarget.children);
const door = true;
doorClick.forEach(function (test) {
test.addEventListener("click", function () {
if (door) {
const doorRandom = Math.floor(Math.random() * 3) + 1;
console.log(doorRandom);
if (doorRandom === 1) {
document.querySelector(".test1").src = "images/friends.png";
} else {
document.querySelector(".test1").src = "images/empty-room.png";
}
}
})
})
<div class="door-wrapper">
<div class=" door-track-container">
<ul class="door-track">
<li class="door-number">
<a href="#"><img class="test1 is-selected" src="images/door.png"></a>
</li>
<li class="door-number">
<a href="#"><img class="test1" src="images/door.png"></a>
</li>
<li class="door-number">
<a href="#"><img class="test1" src="images/door.png"></a>
</li>
</ul>
</div>
</div>