Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to attach click event to slick JS slider next button

I'm trying to attach a javascript event to the slick slider next and back buttons. I've tried inspecting the page and seeing what they're called and calling the event on those classes but it doesn't recognize the click event.

Here is the function I'm trying to call:

var i = 0;
$('.slick-next').click(function(){
    console.log('next clicked');
    i++;
    console.log(i);
    if (i==1) {
        $('#people').css('filter', 'none');
    }
})

I've tried using '.slick-next::before' as well and that didn't work either

like image 397
Bridget Williams Avatar asked Oct 15 '18 00:10

Bridget Williams


People also ask

How do you add the Prev and Next button on slick slider?

$("selector"). slick({ nextArrow: '<button type="button" class="btn btn-primary">prev</button>', prevArrow: '<button type="button" class="btn btn-primary">next</button>' });

How do you add an arrow on slick slider?

CodePen Embed - slick-arrows. $(function(){ $("#slider"). slick({ speed: 1000, dots: true, prevArrow: '<button class="slide-arrow prev-arrow"></button>', nextArrow: '<button class="slide-arrow next-arrow"></button>' }); });

What is carousel in slick?

The carousel is a slideshow for cycling through a series of content, built with CSS 3D transforms and a bit of JavaScript. It works with a series of images, text, or custom markup. It also includes support for previous/next controls and indicators. Official documentation.


2 Answers

Need to add .on() method for the click of the button. That's because these next and previous arrow buttons are added later in the DOM by slick.js, and .on() can process events from descendant elements that are added to the document at a later time.

$('body').on('click', '.slick-arrow', function () {
    alert('click working')
})

here is the codepen example

like image 180
piupaul Avatar answered Sep 17 '22 20:09

piupaul


$('.slick-next').click(function(e){
  console.log(e.target);
  $('#people').css('filter', 'none');
})
like image 25
Piterden Avatar answered Sep 18 '22 20:09

Piterden