for (var i=0 ; i < 4 ; i++){ var img = document.createElement('img');
$(img).attr( { 'id' : 'imageId'+this.id+'_'+i, 'width' : '28px', 'height' : '28px' }); $(img).click ( function(){ alert( i ); }); container.appendChild(img) } elem.appendChild(img); after that, EACH 'img' element created in loop, after click event returns last assigned ' i ' eg. when loop loops 5 times, after each click on created image I get alert( 5 ); how to dynamically assign click event with variables in this case?