0

I'm having problems with some dynamic elements that I'm creating with Jquery.

for(var i = 1; i <= totalPages; i++) {
            var link = $(document.createElement('a')).html(i.toString());
            link.attr("id", "link" + i);
            link.click(function ()
            {
                nextPage(i);
            });
            link.css('cursor', 'pointer');
        }

When I see the page and click in the links they always send the parameter with the last value of the variable "i" (always totalPages + 1).

Anyone knows what am I doing wrong?

1 Answer 1

2

Try this

for(var i = 1; i <= totalPages; i++) {
    var link = $(document.createElement('a')).html(i.toString());
    link.attr("id", "link" + i);
    link.data("index", i);
    link.click(function ()
    {
        nextPage($(this).data("index"));
    });
    link.css('cursor', 'pointer');
}
Sign up to request clarification or add additional context in comments.

Comments

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.