Skip to content
Advertisement

How to add class on the first & last element that has specific class inside multiple same elements class?

Im trying to add class on class on first & last child element with ONLY .active class. I found the code here:

JavaScript

It does work, however my problem is that the firstActiveItem class is only applied inside the first carousel and the lastActiveItem class inside second carousel.

How do i make it applies on all carousel regardless of how many carousel i have with same class?

Here’s my complete code fiddle: https://jsfiddle.net/tarantadakadin101/xf54zsau/39/

Advertisement

Answer

You can iterate over .owl-carousel elements and find the target element from the current .owl-carousel‘s children on every iteration. With this approach no matter how many carousels exists on the page. like this:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement