E
Erwin Moller
Hi,
Considering the following code:
[https://developer.mozilla.org/en/DOM/element.addEventListener]
---------------------------------
if (el.addEventListener){
el.addEventListener('click', modifyText, false);
} else if (el.attachEvent){
el.attachEvent('onclick', modifyText);
}
Comment from Mozilla:
There is a drawback to attachEvent, the value of this will be a
reference to the window object instead of the element on which it was
fired.
---------------------------------
Question:
If I attach events like this, how should I handle the this==window (IE)
problem in the modifyText function?
(I only want to get a reference to the element where this particular
event originated.)
I tried the following approach (which works in IE7 and FF3)
Is this a good/safe approach?
function modifyText(e){
var srcElRef = getSrcElement(e);
// do stuff with srcElRef
}
function getSrcElement(e){
if (e.target){
return e.target;
}
if (e.srcElement){
return e.srcElement;
}
alert("could not find srcelement");
}
And should I prepare for the situation where I alert the "could not find
srcelement"?
Thanks for your time.
Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare
Considering the following code:
[https://developer.mozilla.org/en/DOM/element.addEventListener]
---------------------------------
if (el.addEventListener){
el.addEventListener('click', modifyText, false);
} else if (el.attachEvent){
el.attachEvent('onclick', modifyText);
}
Comment from Mozilla:
There is a drawback to attachEvent, the value of this will be a
reference to the window object instead of the element on which it was
fired.
---------------------------------
Question:
If I attach events like this, how should I handle the this==window (IE)
problem in the modifyText function?
(I only want to get a reference to the element where this particular
event originated.)
I tried the following approach (which works in IE7 and FF3)
Is this a good/safe approach?
function modifyText(e){
var srcElRef = getSrcElement(e);
// do stuff with srcElRef
}
function getSrcElement(e){
if (e.target){
return e.target;
}
if (e.srcElement){
return e.srcElement;
}
alert("could not find srcelement");
}
And should I prepare for the situation where I alert the "could not find
srcelement"?
Thanks for your time.
Regards,
Erwin Moller
--
"There are two ways of constructing a software design: One way is to
make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
-- C.A.R. Hoare