B
Biguana
Hello there,
I'm having a real problem with assigning a function, and I've googled
quite a bit but am not really sure what to look for. (
I have a function (setuplinks) which I am attempting to use to assign
another function (eventfunc) to the onmouseover event handler of a
group of a-tags on my page (recognised by their class). setuplinks is
called when the page is loaded. I have used similar javascript before
and it's worked fine. The difference is that I am trying to add a
parameter which I work out at the runtime of the setup function.
function setuplinks()
{
var atags = document.getElementsByTagName('a');
for (var i=0; i < atags.length; i++)
{
if(atags.className=='groups-period')
{
var x;
x = atags.id.replace('gp','tip');
atags.onmouseover=function(event){eventfunc(event,x);};
}
}
}
What I want the mouseover to do is - eventfunc(event,'tip123')
- or similar (x='tip123' when setuplinks runs), but the function
just seems to assign the reference to x, and all the links all call
eventfunc with the last value of x that was used (on the last loop).
Surely it can't permanently use x as a parameter, because when
eventfunc runs x is out of scope?! But it also doesn't use the value
of x (or only the last value).
Can anyone explain or help? Thank you in advance.
Tim
I'm having a real problem with assigning a function, and I've googled
quite a bit but am not really sure what to look for. (
I have a function (setuplinks) which I am attempting to use to assign
another function (eventfunc) to the onmouseover event handler of a
group of a-tags on my page (recognised by their class). setuplinks is
called when the page is loaded. I have used similar javascript before
and it's worked fine. The difference is that I am trying to add a
parameter which I work out at the runtime of the setup function.
function setuplinks()
{
var atags = document.getElementsByTagName('a');
for (var i=0; i < atags.length; i++)
{
if(atags.className=='groups-period')
{
var x;
x = atags.id.replace('gp','tip');
atags.onmouseover=function(event){eventfunc(event,x);};
}
}
}
What I want the mouseover to do is - eventfunc(event,'tip123')
- or similar (x='tip123' when setuplinks runs), but the function
just seems to assign the reference to x, and all the links all call
eventfunc with the last value of x that was used (on the last loop).
Surely it can't permanently use x as a parameter, because when
eventfunc runs x is out of scope?! But it also doesn't use the value
of x (or only the last value).
Can anyone explain or help? Thank you in advance.
Tim