I am trying to remove a class from a certain element whenever the user clicks a button.
In the process I check whether the element has the class before removing it.
I am using a React component and I'm setting the class variable this.images in componentDidMount Also, the console.log in there confirms the array of images that i need.
componentDidMount() {
this.images = document.querySelectorAll(".project-image");
console.log(this.images);
}
renderNextImage() {
for (let i = 0, ilen = this.images.length; i < ilen; i++) {
if (!this.images[i].classList.contains('d-none')) {
this.images[i].classList.add('d-none');
this.toggleNextImage(i);
}
}
}
toggleNextImage(count) {
let max = this.images.length - 1;
if (count < max) {
count++;
if (this.images[count].classList.contains('d-none')) {
console.log("element contains class d-none"); // THIS WORKS
this.images[count].classList.remove('d-none');
}
}
}
The console log shows up in my console but the actual classList.remove part doesn't work.
What am I doing wrong?
Any help would be appreciated.
if (….contains(…))check, just callremove- it won't error when the class doesn't exist.