5. next slide 2


#1

var main = function () {
$('.dropdown-toggle').click(function() {
$('.dropdown-menu').toggle();
});

$('.arrow-next').click(function() {
    var currentSlide = $('.active-slide');
    var nextSlide = $('.active-slide').next();

    if (nextSlide.length === 0) {
        nextSlide = $('.active-slide');
    }

    currentSlide.fadeOut(600).removeClass('active-slide');
    nextSlide.fadeIn(600).addClass('active-slide');
});

};

$(document).ready(main);

where is that nextslide.length getting its length from?? like i mean what would happen if i try to do this?

console.log(nextslide);

what would be the outcome??
thank you


#2

Hi Tommy,

$('.active-slide') returns a JavaScript object containing a bunch of properties about the element(s) with that class, like ids and tag names, etc.. If you have $('.nonexistent') or something else that will result in nothing coming up, the object is empty. To check if something is empty (or if it exists at all) in JavaScript, finding the length of it is a very common method.
If the object returned by the selector has nothing in it (zero length), you know that the element doesn't exist, and can run code based on that.

Does this help you? :slightly_smiling: