i've this sample code where i have a set of images that i create dinamically and set to each of them an onclick addlistener with certain params. The problem is that all of them when the onclick is set popups always the values of the last image.
Any ideas why i can't set individually and dinamycally a listener to each of them?
Thanks.
Fully functional code below:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title></title>
<script type="text/javascript">
function image() {
var shared = eval([{"img":"9","ext":"jpg"},{"img":"8","ext":"jpg"},{"img":"20","ext":"jpg"},{"img":"24","ext":"png"}]);
var divShared = document.getElementById('image');
for(x in shared){
var img = document.createElement("img");
img.id = "img_"+shared[x].img;
img.style.width = "30px";
img.style.height = "30px";
img.style.cursor = "pointer";
img.style.marginLeft = "15px";
var url = "http://www.kakuylive.com/fotos/user_"+shared[x].img+"."+shared[x].ext;
img.src = url;
document.getElementById('image').appendChild(img);
document.getElementById("img_"+shared[x].img).addEventListener('click', function() { openSharedUserInfo(shared[x].img,shared[x].ext)},false);
}
}
function openSharedUserInfo(id,ext){
alert(id+" :: "+ext);
}
</script>
</head>
<body>
<div id="image" style="margin-bottom:15px"></div>
<div><a href="javascript:image();">click to see image</a></div>
</body>
</html>