Who Caught Me? : The JavaScript techniques which do and don't work (for IE, Windows) in finding out which object caught a bubbled event.

Summary

In client-side JavaScript, if you want to refer to the object who caught a bubbled event in an event-handling function, the following techniques all work...

<div id="foo" onclick="alert(this.outerHTML)"><b>This bold</b> won't interfere.</div>
<div id="foo"><b>This bold</b> won't interfere.</div>
...
foo.onclick=SayMyName;
foo.onclick=function(){SayTheNameOf(this)});
foo.onclick=function(){SayTheNameOf(foo)});
foo.attachEvent('onclick',function(){SayTheNameOf(foo)});
...
function SayMyName(){ alert(this.outerHTML) }
function SayTheNameOf(obj){ alert(obj.outerHTML) }

...but the following two do not work:

foo.attachEvent('onclick',SayMyName);
foo.attachEvent('onclick',function(){SayTheNameOf(this)});

Details

When event bubbling filters a user's action through various superfluous layers, I often want to know which object caught the event (as opposed to which top-level element got in the way). For example, suppose I want to be able to drag a particular <div> around:

It is easy to drag with the mouse.
<div onmousedown="StartDragging()">
	It is <b>easy to drag</b> with the mouse.
</div>
...
function StartDragging(){
	var objectToDrag=event.srcElement;
	alert("I'm going to drag:\n"+objectToDrag.outerHTML);
	...
}
example 1: using event.srcElement

The above doesn't work, because event.srcElement refers to the <b> tag if I click on the words "easy to drag". (Try clicking for yourself.) The correct way to fix this is:

It is easy to drag with the mouse.
<div onmousedown="StartDraggingObject(this)">
	It is <b>easy to drag</b> with the mouse.
</div>
...
function StartDraggingObject(objectToDrag){
	alert("I'm going to drag:\n"+objectToDrag.outerHTML);
	...
}
example 2: using 'this' inline

Frequently I want to be able to attach an event handler programmatically (e.g. when I don't have control over the source HTML):

It is easy to drag with the mouse.
(attachsimplefunction)
It is easy to drag with the mouse.
(attachpassingthis)
It is easy to drag with the mouse.
(attachpassingobject)
<div class="draggable" id="attachsimplefunction">
	It is <b>easy to drag</b> with the mouse.<br>
	(attachsimplefunction)
</div>
<div class="draggable" id="attachpassingthis">
	It is <b>easy to drag</b> with the mouse.<br>
	(attachpassingthis)
</div>
<div class="draggable" id="attachpassingobject">
	It is <b>easy to drag</b> with the mouse.<br>
	(attachpassingobject)
</div>
...
attachsimplefunction.onmousedown=StartDraggingThis;
attachpassingthis.onmousedown=function(){ StartDraggingObject(this) };
attachpassingobject.onmousedown=function(){ StartDraggingObject(attachpassingobject) };
function StartDraggingThis(){
	var objectToDrag=this;
	alert("I'm going to drag:\n"+objectToDrag.outerHTML);
	...
}
examples 3-5: direct handler assignment

However, often I want to use the .attachEvent() method, to ensure I don't run over any existing handlers.
Only one of the below works (creating an anonymous function and passing the object to it):

It is easy to drag with the mouse.
(attachsimplefunction2)
It is easy to drag with the mouse.
(attachpassingthis2)
It is easy to drag with the mouse.
(attachpassingobject2)
<div class="draggable" id="attachsimplefunction2">
	It is <b>easy to drag</b> with the mouse.<br>
	(attachsimplefunction2)
</div>
<div class="draggable" id="attachpassingthis2">
	It is <b>easy to drag</b> with the mouse.<br>
	(attachpassingthis2)
</div>
<div class="draggable" id="attachpassingobject2">
	It is <b>easy to drag</b> with the mouse.<br>
	(attachpassingobject2)
</div>
...
attachsimplefunction2.attachEvent('onmousedown',StartDraggingThis);
attachpassingthis2.attachEvent('onmousedown',function(){ StartDraggingObject(this) });
attachpassingobject2.attachEvent('onmousedown',function(){ StartDraggingObject(attachpassingobject2) });
examples 6-8: using attachEvent()
Gavin Kistner
9:55am EST
2003-Apr-21
The above was originally written and tested with IEWin. Although the above information has not been verified for other browsers, I do have a cross-browser (Mozilla/Netscape 6/IE/others) wrapper method for attaching events freely available.
Gavin Kistner
9:37am EST
2003-Oct-17
I need to update this with proper DOM 2 Events information, using addEventListener and the correct evt.currentTarget element, which obviates the need for all this tomfoolery.
Gavin Kistner
12:59am EST
2006-Mar-31
Sorry, comments have been disabled, because some fuckwad spammer (yes, YOU asshole) has been trying to flood my site with links. I'm tired of deleting them, so until I implement some sort of a captcha or other authentication system, I'm just taking comments offline.

Stupid mean spammer people just suck. A lot.
created 2002-Jan-18page modified 2002-Jan-18