Firefox uses onclick arguments in IE

In firefox, when you add an onclick event handler to a method, the event object is automatically passed to this method. This allows, among other things, to detect which particular item was clicked. for instance

document.body.onclick = handleClick; function handleClick(e) { // this works if FireFox alert(e.target.className); } 

Is there a way to get closer to this in IE? I should be able to detect which element clicked from the event handler in the body element.

+4
source share
4 answers

Here is how I would do it if I cannot use jQuery

 document.body.onclick = handleClick; function handleClick(e) { //If "e" is undefined use the global "event" variable e = e || event; var target = e.srcElement || e.target; alert(target.className); } 

And here is the jQuery solution

 $(document.body).click(function(e) { alert($(this).attr("class")); }); 
+7
source

This is not an approved notation for adding events to dom nodes.

 if (el.addEventListener){ el.addEventListener('click', modifyText, false); } else if (el.attachEvent){ el.attachEvent('onclick', modifyText); } 

It is the recommended designation for binding transition events between browsers.

See:

In addition, when you click on an event, the callback function that is executed contains the "this" object, which is the object that was clicked.

 function foo() { window.open(this.src, '_blank'); } 
+3
source

In IE it is

 e.srcElement 
+2
source

I think IE uses a variable called event . See if this works?

0
source

All Articles