I am working on a for loop that dynamically generates elements within a div. Each element should trigger the same function, but with a unique ID.
for(var i = 0; i < 10; i++)
{
var p = document.createElement("p");
var t = document.createTextNode("asdf");
p.appendChild(t);
p.addEventListener("click", function(e){popup(e, i);}, false);
document.getElementById("someDiv").appendChild(p);
}
Let's assume the function is:
function popup(e, id)
{
//do stuff with the mouse event and get data according to the id
}
Therefore, I require the mouse
event object to work properly.
Currently, every click results in the function being called with the same ID parameter (always sending 10 as the ID, although the mouse
event functions correctly).
Any suggestions or insights would be highly appreciated!