0

i am trying to make a slideshow that counts the amount of images inside a div. and makes a clickable circle for each image.

this is the code that seems to fail:

l = heroimgs.length;
for(var i=0; i<l; i++){
    $('#heronavpoint'+i).click(function(){
        $('.heroimg:visible').fadeOut(800);
        $('.heroimg:eq('+i+')').stop().delay(800).fadeIn();
    });
}
0

1 Answer 1

2

For example, using a closure:

for (var i = 0; i < l; i++) {
    (function (i) {
        $('#heronavpoint' + i).click(function () {
            $('.heroimg:visible').fadeOut(800);
            $('.heroimg:eq(' + i + ')').stop().delay(800).fadeIn();
        });
    }(i));
}

In your case, you could use that:

$('[id^=heronavpoint]').click(function () {
    $('.heroimg:visible').fadeOut(800);
    $('.heroimg:eq(' + $(this).index() + ')').stop().delay(800).fadeIn();
});
Sign up to request clarification or add additional context in comments.

1 Comment

Thx! no idea what is so different other than you using an extra function with a parameter?

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.