0

I tried creating a simple slideshow plugin but it states .next() is not a function.

Uncaught TypeError: currSlider.next is not a function at change (jquery.methodSlider.js:11)

(function ( $ ) {

$('.res-slideshow').methodSlider({
    speed:3000
});
$.fn.methodSlider = function(options) {
    var settings = $.extend({
        speed:1000
    },options);
    this.find('li:first').addClass('top');

    var change = function(){
        var currSlider = this.find('li.top');
        //This doesnt work.
        var nextSlider = currSlider.next();

        nextSlider.addClass('top');
        currSlider.removeClass('top');
    }
    setInterval(change, settings.speed);
    return this;
};

}( jQuery ));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="residence-slider">
    <ul class="res-slideshow">
        <li>
          <img src="img/site_images/home-01.jpg" alt="This is image 1">
        </li>
        <li>
          <img src="img/site_images/home-02.jpg" alt="This is image 2">
        </li>
        <li>
          <img src="img/site_images/home-03.jpg" alt="This is image 3">
        </li>            
    </ul>
</div>
Ram Segev
  • 2,321
  • 2
  • 9
  • 23
Reza San
  • 200
  • 2
  • 12

1 Answers1

1

As Andreas said, the matter comes from your this utilisation.

This is not the dom element as you use it, in jQuery, you have to add a $ before so it's $(this) to get the element.

In your case, since this was not the element, the next() function wasn't available.

I updated your code below :

(function ( $ ) {


$.fn.methodSlider = function(options) {
    var settings = $.extend({
        speed:1000
    },options);
    $(this).find('li:first').addClass('top');
    
    var that = $(this);
    var change = function(){
        var currSlider = that.find('li.top');
        var nextSlider = currSlider.next();

        nextSlider.addClass('top');
        currSlider.removeClass('top');
    }
    setInterval(change, settings.speed);
    return this;
};

$('.res-slideshow').methodSlider({
    speed:3000
});

}( jQuery ));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="residence-slider">
    <ul class="res-slideshow">
        <li>
          <img src="img/site_images/home-01.jpg" alt="This is image 1">
        </li>
        <li>
          <img src="img/site_images/home-02.jpg" alt="This is image 2">
        </li>
        <li>
          <img src="img/site_images/home-03.jpg" alt="This is image 3">
        </li>            
    </ul>
</div>
Nico_
  • 1,051
  • 13
  • 26