Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

jQuery .trigger() handler.

Hi guys! I have this html:

<div class="col-lg-12 fp-properties">
        <h3 class="fp-h3-heading no-top-margin fitTextFP" style="text-align: center;">Een greep uit onze investeringen</h3>
        <div class="customNavigation">
          <a class="btn btn-primary prev">Previous</a>
          <a class="btn btn-primary next">Next</a>
        </div>
        <!-- Owl-Carousel Properties -->
        <div id="owl-properties" class="owl-carousel">
          <?php
            $args = array( 
              'post_type'     => 'properties',
              'posts_per_page' => -1,
              );

            $the_query = new WP_Query( $args );
          ?>

          <?php if ( have_posts() ) : while ( $the_query->have_posts() ) : $the_query->the_post(); ?>  
          <div class="item">

            <?php 

              $image = get_field('property_foto');

              if( !empty($image) ): 

                // vars
                $url = $image['url'];
                $title = $image['title'];
                $alt = $image['alt'];
                $caption = $image['caption'];

                // thumbnail
                $size = 'property-slider';
                $thumb = $image['sizes'][ $size ];
                $width = $image['sizes'][ $size . '-width' ];
                $height = $image['sizes'][ $size . '-height' ];

                ?>

                  <img src="<?php echo $thumb; ?>" class="img-responsive img-rounded" title="AWF Property Example" alt="AWF Property Example" width="<?php echo $width; ?>" height="<?php echo $height; ?>" />

              <?php endif; ?>

          </div>
          <?php endwhile; endif; ?>
        </div> <!-- Owl Carousel -->
      </div>

As you can see, I have a div called customNavigation and have two links inside of that, I want those link two function as Owl Carousel's owl.prev and owl.next buttons, see here: http://owlgraphic.com/owlcarousel/demos/custom.html

I have setup this jQuery:

$(document).ready(function(){
    $(".customNavigation .btn.btn-primary.next").click(function(){
        $("#owl-properties").trigger('owl.next');
    })
    $(".customNavigation .btn.btn-primary.prev").click(function(){
        $("#owl-properties").trigger('owl.prev');
    })
});

and nope, it does not work, what am I missing here? anybody an idea?

Thanks!

3 Answers

Hi Boris,

I haven't used it before either but your code looks like it's correct.

All that seems missing is the call to the Owl initializer function.

$("#owl-properties").owlCarousel();

Try putting that before your click handlers that you've set.

Thanks for checking in! Please my comment above to see how I setup the owl. Thanks!

Hi Boris,

I still have to say that I don't think there is anything wrong with the original code that you have shown, I think the problem lies elsewhere and it could be that you appear to be using version 2 like Aaron mentioned.

V2 is a beta version meant only for testing according to the site. You should not be using this in production. When I try to use it locally I get a javascript error from the carousel file in both firefox and chrome and I can't get the next and previous buttons working. There are probably still bugs in this version and I would recommend you switch back to 1.3.2 if that is in fact what you did. You will have to take out the options though that didn't exist in the older version.

I set up a codepen demo using your code and the 1.3.2 version of owl loaded via cdn. I copied over your code and did not make any changes. The only exception was that I had to guess what the carousel items were but that shouldn't matter. I put "item" div's with images inside.

The autoplay is not working and I don't know why but the "previous" and "next" buttons are working. This is why I don't think there is anything wrong with the code that you have shown us, unless I didn't duplicate something correctly.

I even created the other 2 carousels just in case multiple carousels was causing some kind of conflict.

http://codepen.io/anon/pen/yBbps?editors=101

I take some of that back. I can get the "previous" and "next" buttons working in v2. The problem was that the event names have changed. Now they are next.owl.carousel and prev.owl.carousel. The old ones were owl.next and prev.next

You can see the new event names about 3/4 down the page here: http://www.owlcarousel.owlgraphic.com/docs/api-events.html

If I change the event names in your code then it works with v2. Also, I guess the autoplay wasn't working because I hadn't included the autoplay js file.

I still would recommend not using it unless you understand that you're taking a risk. I would at least test it in all the browsers you care to support and also check out the reported v2 issues to see if any will affect you. https://github.com/OwlFonk/OwlCarousel2/issues

So my revised guess on the problem is that you're using v2 with the old event names.

I have never used owl carousel before, but from the documentation it looks like you should be using the .trigger() method of the owl object instead of the jQuery object. I don't see anywhere that you construct an owl object. I am guessing that you did that in some other block of code. Assuming your owl object is just owl, try the following;

$(document).ready(function(){
    $(".customNavigation .btn.btn-primary.next").click(function(){
        owl.trigger('owl.next');
    })
    $(".customNavigation .btn.btn-primary.prev").click(function(){
        owl.trigger('owl.prev');
    })
});

Edit: The more I look at this, the more I am wondering if you set up an instance of owlCarousel. Do you have anything like the following in your code?

var owl = $(".owl-carousel").owlCarousel();

Thanks you for your answer, I tried it and it doesn't work... I did setup the instance, yes. Here's my total code:

//call Owl-Carousel
$(document).ready(function(){

    $("#owl-properties").owlCarousel({
        nav: false,
        pagination: true,
        loop: true,
        dots: false,
        autoplay: true,
        autoplayTimeout:2000,
        autoplayHoverPause:true,
        margin:10,
        responsiveClass:true,
        responsive:{
            0:  {
                items:2
            },
            450:{
                items:3
            },
            767:{
                items:4
            },
            991:{
                items:5
            },
            1199:{
                items:5
            }
        }
    });

    //Custom Navigation Events
    $(".customNavigation .btn.btn-primary.next").click(function(){
        $("#owl-properties").trigger('owl.next');
    })
    $(".customNavigation .btn.btn-primary.prev").click(function(){
        $("#owl-properties").trigger('owl.prev');
    })

    $("#owl-awf1-properties,#owl-awf2-properties").owlCarousel({
        pagination: true,
        loop: true,
        dots: false,
        autoplay: true,
        autoplayTimeout:2000,
        autoplayHoverPause:true,
        margin:10,
        responsiveClass:true,
        responsive:{
            0:  {
                items:2
            },
            450:{
                items:3
            },
            767:{
                items:4
            },
            991:{
                items:5
            },
            1199:{
                items:5
            }
        }
    });

});

Does that help you? Thanks for helping!

boris kamp - to use owl.prev and owl.next, you need a variable that references an instance of the owl object. I did some checking and built a quick mockup and it looks like this will work:

var owl = $('#owl-properties').data('owlCarousel');

try adding that just before your custom navigation events and let me know if it works.

Hi Aaron! Thank you for putting effort in this. Unfortunately id did not work, I hope I did the right thing. I have this in my code now:

$(document).ready(function(){

    $("#owl-properties").owlCarousel({
        nav: false,
        pagination: true,
        loop: true,
        dots: false,
        autoplay: true,
        autoplayTimeout:2000,
        autoplayHoverPause:true,
        margin:10,
        responsiveClass:true,
        responsive:{
            0:  {
                items:2
            },
            450:{
                items:3
            },
            767:{
                items:4
            },
            991:{
                items:5
            },
            1199:{
                items:5
            }
        }
    });

    //Custom Navigation Events
    var owl = $('#owl-properties').data('owlCarousel');

    $(".customNavigation .btn.btn-primary.next").click(function(){
        owl.trigger('owl.next');
    })
    $(".customNavigation .btn.btn-primary.prev").click(function(){
        owl.trigger('owl.prev');
    })

});

and this piece of html:

        <div class="customNavigation">
          <a class="btn btn-primary prev">Previous</a>
          <a class="btn btn-primary next">Next</a>
        </div>

Do you notice anything I did wrong?

Thanks!

boris kamp - Do you happen to know which version of Owl Carousel you are running? If you aren't sure, you should be able to find it by opening the owl.carousel.js file you are using. I was assuming you are using version 1, but It looks like you are using configuration options from version 2. Here is the code I am using to get version 1 to work:

$(document).ready(function(){

  $("#owl-properties").owlCarousel({
    nav: false,
    pagination: true,
    loop: true,
    dots: false,
    autoplay: true,
    autoplayTimeout:2000,
    autoplayHoverPause:true,
    margin:10,
    responsiveClass:true,
    responsive:{
      0:  {
        items:2
      },
      450:{
        items:3
      },
      767:{
        items:4
      },
      991:{
        items:5
      },
      1199:{
        items:5
      }
    }
  });

  var owl = $('#owl-properties').data('owlCarousel');

  //Custom Navigation Events
  $(".customNavigation .btn.btn-primary.next").click(function(event){
    event.preventDefault();
    owl.next();
  });
  $(".customNavigation .btn.btn-primary.prev").click(function(event){
    event.preventDefault();
    owl.prev();
  });

});

Obviously, since I am using version 1, the configuration options don't work.

If it turns out you are using version 2, this code probably won't work to trigger the prev and next events. If it turns out you are using version 1, this code should work for executing the prev() and next() methods, but you will probably need to redo your configuration options. These are the version 1 options I was able to find: http://owlgraphic.com/owlcarousel/#customizing

Let me know what you find out.

Hi Aaron,

I don't think that there is an owl object in the custom demo that Boris linked to, I think the .trigger method is the jQuery trigger method.

In the example demo they're caching the jQuery object in the owl variable. var owl = $("#owl-demo");

So later when you do this, owl.trigger('owl.next'); you're calling the trigger method on a jquery object.

I suppose they should have followed the convention of putting a $ in front of the owl variable to indicate it's storing a jQuery object.

I think the only difference between Boris' code and the demo was that Boris didn't cache the selection but rather reselected every time and the possibility of incorrect event names.

Jason! You're a hero! You solved two of my main question to finish this project! I was working with V2 indeed and that was a big mistake. I changed to the reliable v1 and now everything works! You solved this question as well as replacing the v2 files caused the carousel to work correctly in both tabs! https://teamtreehouse.com/forum/owl-carousel-issue-with-bootstrap-tabs

THANKS!!!