1

I need to do something like this:

for(int i =0; i< results.length; i++){
    $('ul').append('<li>'+results[i].name+'</li>').bind('click',function(){
        doSomething(results[i]);
    })
}  

but when the click happens, the object passed to doSomething is undefined. How do I solve this?

1

3 Answers 3

3

results[i] is only valid variable at the time machine is inside the for loop, later on, when event handler is triggered, result[i] is undefined.

That's why you need to ensure results[i] gets saved as variable in context of a function.

for(var i=0; i< results.length; i++){
    (function(result) {
        $('<li>'+result.name+'</li>').appendTo($('ul')).bind('click',function(){
            doSomething(result);
        })
    })(results[i]);
}

Here's working example http://jsfiddle.net/T9aUq/.

Edit: Actually to make it really work, I had to change the way events are registered - at first, click handler was binded to elements matching $('ul') instead of new li element (since append() function of jQuery doesn't return appended element, but rather sticks to previous scope (which was ul).

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

1 Comment

I think you're missing braces? (function(result) { ... })(results[i]);
0

save your element first!

for(var i =0; i< results.length; i++){
    var element = results[i];
    var li = $('<li/>').html(element.name);
    $('ul').append(li);
    li.click(function(){
        doSomething(element);
    });
}  

3 Comments

To elaborate on this answer; the issue is that i is constantly changing.
This won't work, because just as i changes, so does element. See the other answer that addresses the issue with closures.
Your code is broken. For one, there's typo int instead of var, for two, the resulting handler would always fire doSomething function with last element of results array as argument. Check it out here: jsfiddle.net/9hTBa
0

I actually ran into this same problem recently... Silly is right, however the variables need some form of encapsulation:

for(var i =0; i< results.length; i++) doThisFirst(i);
function doThisFirst(index) {
    $('ul').append('<li>'+results[index].name+'</li>').bind('click',function(){
        doSomething(results[index]);
    })
}

If that doesn't work... Make sure you don't have a loop inside a loop with the same varname for the index:

for (var i = 0; ...) {
    for (var i = 0; ...) {
    }
}

1 Comment

I'm sorry, I did not realize WTK had posted the answer for this already.

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.