How can I get current active slide, and when change slide I get the new one?
I’ve try with:
$('#carouselExampleIndicators').bind('slid', function() {
let currentIndex = $('button.active').index() + 1;
alert(currentIndex)
});
but not working
This is JsFiddle: https://jsfiddle.net/moku23/65p0r9f1/6/
2
Answers
If you read the Bootstrap Carousel documentation, you can see that the correct event is
slid.bs.carousel
, and also that it provides 4 additional properties to the event that’s passed to the handler. One of which,from
, is the index of the current slide.In addition,
bind()
is deprecated and should not be used. Change your code to useon()
instead. I’d also check to make sure you’re using an up to date version of jQuery. The latest version is 3.6.0.Here’s a full working version of your fiddle:
Use on() instead of bind()