I have a for loop that generates elements into a div, and I want to have each element calling the same function but with an 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 say, the function is:
function popup(e, id)
{
//do stuff with the mouse event and get data according to the id
}
so I need the mouse
event object.
This current code does make the elements, but every click calls the function with the same id as parameter (10 is being sent as the id, the mouse
event works fine).
Any idea is appreciated!