> want to know how to refer to the object that triggered the function?
from within the function itself..
Oh I see now... It depends on how your function was called: by event
capturer or by another function during normal program flow.
If by event capturer, then you simply *cannot* do it.
At least nothing reliable that would work for all browsers. You can do:
function myFunction(e) {
// "e" argument is important placeholder
// in case of working in NN/DOM event model
// IE sets global "event" variable for you
var myCaller = (e)? e.currentTarget : event.srcElemen t;
}
Of course you have to add event capturers programmically in this case.
You cannot do <element onclick="myFunc tion()">,
Empty parenthesis will kill the "e" and the script will fail for FF &
Co
You have to:
obj.onclick = myFunction;
or
using addEventListene r (DOM) / attachEvent (IE) methods
This works fine as long as there is no more elements below the element
that capturing events. Otherwise all this mess fails onto the ground.
In FF you still can get the right object by using currentTarget. But in
IE event.srcElemen t will point to the first element in the bubble
history. So in the majority of situations your only option is to use
intrinsic capturers and "manually" forward the right object to the
function: <element onclick="myFunc tion(this)">
If your function is called by another function during normal program
flow, you can check the caller property.
function myFunction {
if (myFunction.cal ler != null) {
myCaller = myFunction.call er
}
else {
// I'm the first one!
}
}
P.S. There is a lot of bubbles in docs about "caller is deprecated".
But deprecation doesn't mean "removal", it means "substituti on". As
long as *no one* provided a reasonable substitution for caller, we just
keep using it. It's supported by all main browsers.