HTML
<body>
<div id='test' onClick="load()">
2
</div>
</body>
Javascript
load = function() {
test.innerHTML = "";
for (var i = 0; i < 5; i++) {
var p = document.createElement("p");
p.innerHTML = "Link";
p.onclick = function () {
alert("LINK NR: " + i)
}
document.getElementById('test').appendChild(p);
}
}
In the code above, why does the function always return the last value
?
Here is a fiddle for the code.