const carSelector = document.querySelectorAll(".cars-select");
const car = document.querySelectorAll(".car");
carSelector.forEach((selected, index) => {
selected.addEventListener("click", () => {
car.forEach((car) => {
car.classList.remove("show");
});
car[index].classList.add("show");
});
});
This is showing all the time first element, doesn’t matter on which select i click.
I think that "index" is == 0; but idk how to change it.
2
Answers
The problem with your code is that the index variable doesn’t work as expected. To fix this, we can use the
function
keyword.Please mark as correct answer if it helped.