chump2877
02-16-2007, 05:31 PM
I'm trying to replace some inline event handlers in some code I wrote...
So I wrote a function that attaches the events in an external JS file...this has worked for me before, but this time, I've noticed that the variable "num" in my JS always evaluates to "8"....I'd tried passing "num" as a parameter in my anonymous functions, but that just caused "num" to evaluate to something like "[event object]" (if memory serves me)...
I need "num" to evaluate as a number between 1-8, inside my anonymous functions, depending on the iteration of the for loop...here is the code and thanks for any help:
The markup:
<div id="menu">
<img id="logo" src="name_image.jpg" alt="" />
<h2 style="top:92px" id="cat1" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=1">********</a></h2>
<h2 style="top:116px" id="cat2" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=2">********</a></h2>
<h2 style="top:140px" id="cat3" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=3">********</a></h2>
<h2 style="top:164px" id="cat4" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=4">********</a></h2>
<h2 style="top:188px" id="cat5" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=5">********</a></h2>
<h2 style="top:212px" id="cat6" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=6">********</a></h2>
<h2 style="top:236px" id="cat7" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=7">********</a></h2>
<h2 style="top:260px" id="cat8" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=11">********</a></h2>
</div>And the JS:
function configMenuLinks()
{
var menuDiv = document.getElementById('menu');
var menuLinks = menuDiv.getElementsByTagName("a");
var num = 0;
for (i=0; i<menuLinks.length; i++)
{
num = i + 1;
menuLinks[i].onclick = function()
{
if (num == menuLinks.length)
showTransDivs('div11');
else
showTransDivs('div'+num);
return false;
}
menuLinks[i].onmouseover = function()
{
alert('cat'+num);
linkFade('cat'+num,1);
}
menuLinks[i].onmouseout = function()
{
linkFade('cat'+num,2);
}
}
}
So I wrote a function that attaches the events in an external JS file...this has worked for me before, but this time, I've noticed that the variable "num" in my JS always evaluates to "8"....I'd tried passing "num" as a parameter in my anonymous functions, but that just caused "num" to evaluate to something like "[event object]" (if memory serves me)...
I need "num" to evaluate as a number between 1-8, inside my anonymous functions, depending on the iteration of the for loop...here is the code and thanks for any help:
The markup:
<div id="menu">
<img id="logo" src="name_image.jpg" alt="" />
<h2 style="top:92px" id="cat1" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=1">********</a></h2>
<h2 style="top:116px" id="cat2" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=2">********</a></h2>
<h2 style="top:140px" id="cat3" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=3">********</a></h2>
<h2 style="top:164px" id="cat4" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=4">********</a></h2>
<h2 style="top:188px" id="cat5" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=5">********</a></h2>
<h2 style="top:212px" id="cat6" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=6">********</a></h2>
<h2 style="top:236px" id="cat7" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=7">********</a></h2>
<h2 style="top:260px" id="cat8" class="fade1"><a href="<? echo $_SERVER['PHP_SELF']; ?>?div=11">********</a></h2>
</div>And the JS:
function configMenuLinks()
{
var menuDiv = document.getElementById('menu');
var menuLinks = menuDiv.getElementsByTagName("a");
var num = 0;
for (i=0; i<menuLinks.length; i++)
{
num = i + 1;
menuLinks[i].onclick = function()
{
if (num == menuLinks.length)
showTransDivs('div11');
else
showTransDivs('div'+num);
return false;
}
menuLinks[i].onmouseover = function()
{
alert('cat'+num);
linkFade('cat'+num,1);
}
menuLinks[i].onmouseout = function()
{
linkFade('cat'+num,2);
}
}
}