jquery trigger click in a loop

skullred

New Member
Hi I am trying to append facebook friends thumbnail in list item and add trigger on each of them. but now it trigger the click but it's only getting fbid of the last appended item inside the click callback. How can I attach click event on each of them correctly?\[code\]for(var i=0;i<obj.photo.length;i++) {var img=$('<img src="https://graph.facebook.com/'+obj.photo['fb_id']+'/picture" />');var anchor=$('<a href="http://stackoverflow.com/questions/15737281/#" id="'+obj.photo['fb_id']+'"></a>');var li = $('<li></li>');var fbul = $('#fb_friends');anchor.append(img);li.append(anchor);fbul.append(li);anchor.click(function(){ alert(anchor.attr('id'));});}\[/code\]
 
Back
Top