0

I have this piece of code:

$(document).on("click", "#breadNavMain", function() {
    for(var i = 0; i < getActiveSlides().length; i++) {
        $("#studentWrapper").trigger("click");
    }
});

The method getActiveSlides() will return the slides (as an array) currently activated for my application. What's import is that I am getting the number of slides that are active. For anything over 1 active slide, the loop above does not work. If I have 3 active slides (for example), it will only call the .trigger() method once. Within my click handler, if I instead use:

$("#studentWrapper").trigger("click");
$("#studentWrapper").trigger("click");
$("#studentWrapper").trigger("click");

It will work just fine. The problem is that I don't know how many times I'll need to call the .trigger() method so I am unable to do this manually. I wanted to call it inside of a loop like I tried to above. Is there anyway to get .trigger() to work inside of a loop?

2
  • Are you sure getActiveSlides().length is returning something? Maybe add an alert(getActiveSlides().length) before the loop and see what it says. Commented Jan 30, 2012 at 22:00
  • Yeah, I made sure that the method was returning a correct number each time by doing a console.log(getActiveSlides().length). It returned the correct number each time for all the different possible combinations of slides that could be active in my application. Commented Jan 30, 2012 at 22:05

1 Answer 1

1

It should actually work fine, but I am skeptical that something is wrong inside getActiveSlides method. May be after first click it is not returning the correct number of active slides. You can take its return value into a variable and then execute your code. Try this.

$(document).on("click", "#breadNavMain", function() {
    var activeSlidesLen = getActiveSlides().length;
    for(var i = 0; i < activeSlidesLen; i++) {
        $("#studentWrapper").trigger("click");
    }
});
Sign up to request clarification or add additional context in comments.

2 Comments

I will give that a try tomorrow once I am back at work :) I made sure that the method was returning a correct number each time by doing a console.log(getActiveSlides().length) so I figured it would work just fine. Will get back to you on this...
Your suggestion about putting the length into a variable worked. I still don't really understand why getActiveSlides().length didn't work for the counter though. I've definitely done things like that before in other programs...

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.