I'm new to this, and would really appreciate if someone could explain:
Why do i get Uncaught TypeError: Cannot set property 'className' of undefined with the below code ?
require(
["jquery"],
function($) {
function Slider() {
this.slides = document.querySelectorAll('#slides .slide');
this.currentSlide = 0;
this.slideInterval = 0;
}
Slider.prototype.startSlider = function( slideDuration ) {
this.slideInterval = setInterval( this.nextSlide, slideDuration );
};
Slider.prototype.nextSlide = function() {
this.slides[ this.currentSlide ].className = 'slide';
this.currentSlide = ( this.currentSlide+1 ) % this.slides.length;
this.slides[ this.currentSlide ].className = 'slide showing';
};
Slider.prototype.goToThis = function() {
};
Slider.prototype.goToNext = function() {
};
Slider.prototype.goToPrev = function() {
};
var slider = new Slider();
slider.startSlider( 1000 );
});
When i do it without the Slider() class it works fine.
var slides = document.querySelectorAll('#slides .slide');
var currentSlide = 0;
var slideInterval = setInterval(nextSlide,2000);
function nextSlide() {
slides[currentSlide].className = 'slide';
currentSlide = (currentSlide+1)%slides.length;
slides[currentSlide].className = 'slide showing';
}