The problem is that dst and vst are changing in your loop. Therefore, when your click handler is running, it uses the final versions of dst and vst, not the values ββthat they had when creating the click handler.
You need a copy of dst and vst, which you can make by creating a new context. Example
function make_handler(vst, dst) { $(vst).click(function(){ var vid_id = $(dst).html(); console.log(vid_id); $.post("/nodes/iframize/", {video_id: vid_id}); }); } $(function() { for ( i=0; i < parseInt(ids); i++){ var vst = '#'+String(img_arr[i]); var dst = '#'+String(div_arr[i]); make_handler(vst, dst); } });
You can do this inline, but you need a function that takes vst and dst as arguments because they are copied and the context is preserved when the callback occurs.
Edit: by "do inline" I mean replacing the call to make_handler () as follows:
function(vst,dst) { ... } (vst, dst);
source share