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
Post a Comment