ZimZangZoom
Programmer
- Dec 17, 2004
- 26
Hello,
I am looking through an array.
I want to set the onClick attribute to different elements during the loop.
Here is what I have so far
But when I click on the div, the "name" is always the last result of the loop.
You can run the code and see what I mean.
How can I make the onclick even different each time?
I am looking through an array.
I want to set the onClick attribute to different elements during the loop.
Here is what I have so far
Code:
<html>
<head>
<title>My Doc</title>
<script>
function SetTabs()
{
var TabArray = new Array();
TabArray[0] = "FirstTab";
TabArray[1] = "SecondTab";
TabArray[2] = "ThirdTab";
for (i in TabArray)
{
var tabId =TabArray[i] + "_ID";
var tabName = TabArray[i]
document.getElementById(tabId).onclick = function()
{
ShowTab(tabName);
}
}
}
function ShowTab(name)
{
alert(name)
}
</script>
</head>
<body onload="SetTabs();">
<div id="FirstTab_ID">One</div>
<div id="SecondTab_ID">Two</div>
<div id="ThirdTab_ID">Three</div>
</body>
</html>
But when I click on the div, the "name" is always the last result of the loop.
You can run the code and see what I mean.
How can I make the onclick even different each time?