Hello
I try to hanlde events centrally for avoiding the need to write four event
handler attributes to every link. The task is changing the class name of a
link's containing <div> element. After lots of googling I came to this:
if (document.getEl ementsByTagName ) {
a = document.getEle mentsByTagName( "a");
for (var i=1; i<a.length; i++) {
var element = a[i];
element.onmouse over = function() { element.parentN ode.className =
"aktiv"; };
element.onMouse Out = function() { element.parentN ode.className =
"passiv"; };
element.onFocus = function() { element.parentN ode.className =
"aktiv"; };
element.onBlur = function() { element.parentN ode.className =
"passiv"; };
}
}
The script does not throw any errors neither in IE 6 nor in Firefox, but it
does only change the last element to aktiv, regardless of which element is
hovered or unhovered.
I thought of the a[i] overwriting the element variable in every loop and
also tried calling a[i] directly, and other things such as
var element = document.getEle mentById(a[i].id);
or an for-in-loop; the result is only the same.
Is my approach totally wrong, or is there anything I am missing?
Thanks for any hint
Markus
I try to hanlde events centrally for avoiding the need to write four event
handler attributes to every link. The task is changing the class name of a
link's containing <div> element. After lots of googling I came to this:
if (document.getEl ementsByTagName ) {
a = document.getEle mentsByTagName( "a");
for (var i=1; i<a.length; i++) {
var element = a[i];
element.onmouse over = function() { element.parentN ode.className =
"aktiv"; };
element.onMouse Out = function() { element.parentN ode.className =
"passiv"; };
element.onFocus = function() { element.parentN ode.className =
"aktiv"; };
element.onBlur = function() { element.parentN ode.className =
"passiv"; };
}
}
The script does not throw any errors neither in IE 6 nor in Firefox, but it
does only change the last element to aktiv, regardless of which element is
hovered or unhovered.
I thought of the a[i] overwriting the element variable in every loop and
also tried calling a[i] directly, and other things such as
var element = document.getEle mentById(a[i].id);
or an for-in-loop; the result is only the same.
Is my approach totally wrong, or is there anything I am missing?
Thanks for any hint
Markus
Comment