0

Having a bit of a dumb moment right now and can't figure out why this is not working. It's been awhile since doing anything with jquery.

var owl = $('.owl-main');
var button = $('.owl-button');

owl.owlCarousel({
    dots:false,
    margin:15,
    nav:true,
    navText: [
        '<span class="o-page">Prev</span>',
        '<span class="o-page">Next</span>'
    ],
    responsive:{
        0:{
            items:1
        }
    },
    onChanged: callback
})

function callback(event) {
    console.log(event.item.index);
    console.log(button);

    button.removeClass('active');
    //$(".owl-button[data-id='"+event.item.index+"']").addClass('active');
    button.find("[data-id='" + event.item.index + "']").addClass('active');
}

My issue is button.find("[data-id='" + event.item.index + "']").addClass('active'); does not add the 'active' class yet the commented out code above it, $(".owl-button[data-id='"+event.item.index+"']").addClass('active'); works perfectly fine.

button and event.item.index are reporting correctly so why is that particular line doing nothing for me?

1
  • Can you show your Html too? Commented Oct 30, 2016 at 7:00

1 Answer 1

1

The issue is find() .. You are using it the wrong way.. it will actually search for the child element within the button with the given attribute. But what you want is to find a button with the given attribute.

  • The commented code is the way you have to do it.

Edit: If you want to use the stored array of button itself to get this working then you can use filter() in place of find

Here is a reference http://api.jquery.com/filter/

button.filter("[data-id='" + event.item.index + "']").addClass('active');

Sign up to request clarification or add additional context in comments.

3 Comments

So there is no way to use my stored var button = $('.owl-button'); in that commented line that works?
Perfect - that is what I was looking for. Thanks for clearing that up!
Glad I could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.