Destroy multiple sliders at once

Asked

Viewed 123 times

2

I want to destroy all my sliders when the page hit the size of 768px. For this I made this code below:

if($(window).width() >= 768){
    $(".owl-carousel-linhas").data('owlCarousel').destroy();
}

I have on my page several sliders so:

<div class="owl-carousel-linhas">
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
    <div class="item"></div>
</div>

<div class="owl-carousel-linhas">
   <div class="item"></div>
   <div class="item"></div>
</div>

<div class="owl-carousel-linhas">
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
   <div class="item"></div>
</div>

And the code I made above destroys only the first slider, the rest not.

2 answers

1

Based on your comment, I’m going to make a change that might solve your problem by putting this code inside your if:

while($(".owl-carousel-linhas").length > 0){
    $($(".owl-carousel-linhas")[0]).data('owlCarousel').destroy();
}

Check if this solves your problem.

  • 1

    I’ve tried that. But as he destroys the first, he can’t find the second... that’s what it looks like. I’ll simulate the error to show you. Uncaught TypeError: e.data is not a function.

1


I managed to fix it. It was simple, until.

$(".owl-carousel-linhas").each(function (index, obj){
    $(this).data('owlCarousel').destroy();
});

I switched the obj for $(this).

  • Here could be $(obj)[0].data() also. Living and letting go.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.