I have a problem with this code in my slider. I need to remove a class from all array elements. The length of the array may vary. this code works:
updateGallery() {
this.carouselArray.forEach((el, i ) => {
el.classList.remove('gallery-item-1');
el.classList.remove('gallery-item-2');
el.classList.remove('gallery-item-3');
el.classList.remove('gallery-item-4');
el.classList.remove('gallery-item-5');
});
let carouselSliceLength = this.carouselArray.slice().length;
this.carouselArray.slice(0, carouselSliceLength).forEach((el, i) => {
el.classList.add(`gallery-item-${i+1}`);
});
}
but this code doesn't work:
updateGallery() {
this.carouselArray.forEach((el, i ) => {
el.classList.remove(`gallery-item-${i+1}`);
});
let carouselSliceLength = this.carouselArray.slice().length;
this.carouselArray.slice(0, carouselSliceLength).forEach((el, i) => {
el.classList.add(`gallery-item-${i+1}`);
});
}
Why?