I'm trying to dynamically create a list images from a JSON array that will invoke a JavaScript function upon clicking one of the images. The function will send a request to the server, flagging that image. I currently have the following:
var GLOBAL_POST_ID_LIST = [];
function createList(jsonData) {
for (var i = 0; i < jsonData.posts.length; i++) {
var curPostId = jsonData.posts[i].post_id;
// Check if the current post is in the list
if ($.inArray(curPostId, window.GLOBAL_POST_ID_LIST) < 0) {
jQuery('<img/>', {
id: curPostId,
src: jsonData.posts[i].image_url,
selected: false,
onclick: onPostClick(curPostId)
}).appendTo('#postDiv');
// At the current post to the list
window.GLOBAL_POST_ID_LIST.push(curPostId);
}
}
}
However, the onclick function is called immediately as the object is initialized, and not when the object is clicked on. How can I caused the onPostClick to be invoked with the correct post_id when any given post is initialized?