I am attempting to create a basic animation that involves changing an image from A to B to C when a button is clicked. However, I am encountering an issue with the error message
Cannot read properties of undefined (reading 'classList')
. I am puzzled as I have successfully used a similar structure for a carousel in the past without any problems.
My goal is to switch the elements from "seed" to Bootstrap's ".d-none".
var javaButton = document.getElementById("button-trigger");
const track = document.querySelector(".plant-wrapper");
const slides = Array.from(track.children);
const targetIndex = slides.findIndex;
const hidden = document.querySelector(".d-none");
const seeds = document.querySelector(".seed");
javaButton.addEventListener("click", moveToSlide);
function moveToSlide(slides, seeds, hidden, targetIndex) {
if (targetIndex === 0) {
seeds.classList.add("is-hidden");
hidden.classList.remove("is-hidden");
} else if (targetIndex === slides.length - 1) {
seeds.classList.remove("is-hidden");
hidden.classList.add("is-hidden");
} else {
seeds.classList.remove("is-hidden");
hidden.classList.remove("is-hidden");
}
};
<button type="button" id="button-trigger">Explore!</button>
<div class="plant-wrapper">
<img src="images/seed.png" class="seed mx-auto d-block" id="seed1">
<img src="images/sprout.png" class="mx-auto d-block d-none" id="seed2">
<img src="images/stem.png" class="mx-auto d-block d-none" id="seed3">
<img src="images/pot.png" class="pot mx-auto d-block">
</div>