Tell me, please, a way to bring the jquery code in a normal form:

$('.go0').click(function(){ carousel.trigger('owl.goTo', 0) }); $('.go1').click(function(){ carousel.trigger('owl.goTo', 1) }); $('.go2').click(function(){ carousel.trigger('owl.goTo', 2) }); $('.go3').click(function(){ carousel.trigger('owl.goTo', 3) }); $('.go4').click(function(){ carousel.trigger('owl.goTo', 4) }); $('.go5').click(function(){ carousel.trigger('owl.goTo', 5) }); $('.go6').click(function(){ carousel.trigger('owl.goTo', 6) }); $('.go7').click(function(){ carousel.trigger('owl.goTo', 7) }); $('.go8').click(function(){ carousel.trigger('owl.goTo', 8) }); 

.go0 can be infinitely many elements ( .go0 , .go1 , etc.), and it is not normal to write them in this form.

Unfortunately, I did not find how to write it in the form with n + 1 or something like that.

    1 answer 1

    Solution "not including the brain":

     for (var ix = 0; ix < 9; ix += 1) { $('.go' + ix).click(function (ix) { return function() { carousel.trigger('owl.goTo', ix); }; }(ix)); } 

    Now the normal solution:

    Usually, the elements of a carousel switch have a common ancestor and the elements themselves follow it in a row. Therefore, it is enough to find the descendant number of the ancestor, which is a trivial task in jQuery. Suppose that all switches have the go class set:

     $('.go').click(function() { carousel.trigger('owl.goTo', $(this).index()); }); 

    If the .go elements are still each in its own wrapper, then instead of $(this) you will need to specify the ancestor, whose position within its ancestor can give the slide number.

    • Thank! Instead of the number 9 (here ix <9;) - you can put any number - and this will be the maximum number to which the script will work? - den69
    • yes, that is exactly the case - LamerXaKer
    • @ den69 Updated answer - tutankhamun
    • Thank you Everything works as it should:) - den69