Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internet Explorer and JavaScript event currentTarget

Is there a way to take the current target of an event with IE 7 or 8?

With other browser (firefox, opera, chrome etc.) we can use event.currentTarget or also we can use the this keyword to refer to the object processing the event.

But in Internet Explorer we don't have currentTarget property and the this refers to window object!

So how can I do that?

like image 481
xdevel2000 Avatar asked May 13 '09 11:05

xdevel2000


People also ask

What does currentTarget do in Javascript?

The currentTarget read-only property of the Event interface identifies the current target for the event, as the event traverses the DOM. It always refers to the element to which the event handler has been attached, as opposed to Event.

What is the difference between E target and e currentTarget?

currentTarget tells us on which element the event was attached or the element whose eventListener triggered the event. event. target tells where the event started.

Why is event target undefined?

You're likely getting this error because you're trying to get a name attribute on elements that don't have a name attribute. For example; input, textarea, and form elements are naturally able to get a name attribute. But elements like div, span doesn't.


2 Answers

You can do something like

target = (event.currentTarget) ? event.currentTarget : event.srcElement; 

Although as @Marc mentioned you can use a JQuery framework that normalizes the event for you.

like image 53
alvincrespo Avatar answered Sep 20 '22 14:09

alvincrespo


I had similar problem. I solved it using keyword this as stated in an article on brainjar.com

To get the equivalent of the currentTarget property in IE, use the this keyword as an argument when setting the event handler in a tag.

...

function myHandler(event, link) { ... }

On the same page you can find the following table :

enter image description here

like image 39
gpasse Avatar answered Sep 16 '22 14:09

gpasse