Search code examples
javascriptjqueryowl-carousel-2

Set active item in owlCarousel


I have a owl carousel with 3 items, the carousel displays one item per page. Inside each item I have html inputs.

I want to programmatically change the active item so I can make focus on input inside it.

Here is the initial configuration:

var owl = $("#selector").owlCarousel({
    slideSpeed: 300,
    paginationSpeed: 400,
    items: 1,
    itemsDesktop: false,
    itemsDesktopSmall: false,
    itemsTablet: false,
    itemsMobile: false,
    dots: false,
    touchDrag: false,
    mouseDrag: false
});

And the html looks like this:

<div id="selector" class="owl-carousel owl-theme">
    <div class="item">
        <input type="text" name="name1" id="input1">
     </div>

    <div class="item">
        <input type="text" name="name2" id="input2">
     </div>

    <div class="item">
        <input type="text" name="name3" id="input3">
     </div>
</div>

For example: if I want to make focus on input3, I have to set the current page to 3 and then set the focus.


Solution

  • to.owl.carousel the event goes to a position.

    Parameter: [position, speed]

    $("#selector").trigger("to.owl.carousel", [2, 1])
    

    As per the events docs: http://owlcarousel2.github.io/OwlCarousel2/docs/api-events.html