Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

188
Views
Trying to change classes on button click

I'm trying to do a simple animation where an image changes from A to B to C on the click of a button. I'm getting stuck at Cannot read properties of undefined (reading 'classList'), but I can't figure out what I did wrong; I used a similar structure for a carousel without any issues.

I'm just trying to change 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">Check it out!</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>

about 4 years ago · Santiago Trujillo
2 answers
Answer question

0

Problem is that you do not pass arguments to moveToSlide function

You can solve removing arguments of moveToSlide function (function will read variables declared above)

function moveToSlide() {
  // Code...
}

Or passing values to function (best option)

javaButton.addEventListener("click", () => moveToSlide(slides, seeds, hidden, targetIndex))
about 4 years ago · Santiago Trujillo Report

0

If your ultimate goal is to create a slider, there's a better way to do it.

w3schools have a demo of it: https://www.w3schools.com/howto/howto_js_slideshow.asp

Here is my way to make a slider:

index.html

<button type="button" id="button-trigger" onclick="changeImg()">Check it out!</button>

<div class="plant-wrapper" id="plant-wrapper">
</div>

index.js

var index = 0;

const imageArray = [
    "images/1.png",
    "images/2.png",
    "images/3.png",
    "images/4.png"
];

function changeImg(){
    (index == imageArray.length - 1 ? index = 0 : index++);
    showImage();
}

function showImage(){
    document.getElementById("plant-wrapper").innerHTML = '<img src="' + imageArray[index] + '" class="mx-auto" />';
}   

window.onload = function() {
    showImage()
}
about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!