I've got follow source code:
<html>
<head><script type="text/javascript">
function test()
{
var links = document.getElementsByTagName('a');
for(var i=0; i< links.length; i++)
{
var link = links[i];
if(link.href)
{
link.onclick = function() {alert("temp" + i); return false;};
};
};
};
</script>
</head>
<body onLoad="test()">
<p><a href="javascript.html">test1</a></p>
<p><a href="javascript.html">test2</a></p>
<p><a href="javascript.html">test3</a></p>
</body>
</html>
Now, if I click on each link, the browser shows me the same result
temp3
I used Firebug to profile the code and I figured out that he takes the last value of variable i. My questions are 1st Why does the browser use the always the last value from the variable? 2nd How have I change the source code, so the browser will show me 'temp1', 'temp2', etc. ?
Thanks