jquery and javascript's closure -


i having code below , javascript's closure anonymous functions giving me headache.

for (var = 0, len = sites.length ; < len ; i++) {   $("#thumb"+i).click(function() { $("#shader").show(); $("#thumbinfo"+i).show(); alert("#thumbinfo"+i); });   $("#thumbinfo"+i+" .xbutton").click(function() { $("#shader").hide(); $("#thumbinfo"+i).hide(); }); } 

due closure, 5 (the sites array has 5 elements), click handlers refer same id.

any workaround?

you loop jquery's each().

$.each(sites, function(i) {   $("#thumb"+i).click(function() { $("#shader").show(); $("#thumbinfo"+i).show(); alert("#thumbinfo"+i); });   $("#thumbinfo"+i+" .xbutton").click(function() { $("#shader").hide(); $("#thumbinfo"+i).hide(); }); }); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -