3

i have the following code :

$(document).ready(function () {
    for (i = 1; i <= number_of_banners; i++) {
    var selector = "#link_" + i;
    $(selector).click(function () {
        alert(i);
        });
    }
});

but the alert() can't get the "i" variable from the for loop. How can I use the i variable of for loop inside the .click function ?

3
  • A classical one:) I guess this has been asked a dozen times on SO. Commented Sep 18, 2012 at 6:05
  • 1
    The problem you're having is that the .click() handlers are using the i variable from the for loop - but all of them are referencing the same, live i variable and by the time clicks occur the for loop has completed and i is equal to number_of_banners. (Only a dozen @Christoph? Probably at least a dozen this month...) Commented Sep 18, 2012 at 6:07
  • possible duplicate of Assign click handlers in for loop Commented Feb 10, 2014 at 22:14

5 Answers 5

5

you can use this code :

$(document).ready(function () {
    for (var i = 1; i <= number_of_banners; i++) {
        var selector = "#link_" + i;
        $(selector).on('click', {id: i}, function (e) {
            alert(e.data.id);
        });
    }
});

you should use on method and use click argument in it instead of using onclick method

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

Comments

4

Using jQuery .on(event, data, handler) you can do it easily.

$(document).ready(function () {
    for (var i = 1; i <= number_of_banners; i++) {
        var selector = "#link_" + i;
        $(selector).on('click', {id: i}, function (e) {
            alert(e.data.id);
        });
    }
});

Working sample

Comments

3

Might this happen be due the fact of the JavaScript hoisting JavaScript Scoping mechanism??

For instance:

doesn't work as JavaScript uses function scope rather than block scope as we're usually accustomed from other languages like Java and C#. In order to make it work, one has to create a new scope by explicitly creating a new anonymous function which then binds the according variable:

I know this doesn't directly answer the question above, but might still be useful though for others stumbling over this question.

3 Comments

It's not a hoisting issue, and the scoping issues discussed in the article you link to don't really cover it either. It's more a closure issue (noting though that i is global in the question), and your second fiddle illustrates one way to get it to work - why don't you update your answer to talk about why it works?
@nnnnnn Uhps, sorry, you're right. As I just quickly posted this answer I wrongly added the hoisting stuff, which of course is more a scoping problem (due to the JavaScript function rather than block scope) as you correctly mentioned. I'll update my answer
+1 for the second example of variable binding...... Though an answer that mentions hoisting shouldn't have var statements anywhere but on the first line of a function ;)
1

I think you can pass it as a parameter into the anonymous function as long as the function is declared within a scope that can access i.

function (i) {
   alert(i);
}

2 Comments

That won't work: that function is a callback for the click handler, and jQuery doesn't know to pass that i value in when it calls the function.
@nnn it may work if he puts the func around the handler binding and not inside.
1

a quick solution would be to use the eventData and store the current i in that:

$(document).ready(function () {
    for (var i = 1; i <= number_of_banners; i++) {
        var selector = "#link_" + i;
        $(selector).bind('click', i, function (e) {
            alert(e.data);
        });
    }
});

if you are using jquery 1.7+ then use on instead of bind

2 Comments

You can also assign a class to your banners and bind the listener to the class! I love jQuery! :D
@nmenego yeah, but the author has ids.

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.