Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between e.target and e.currentTarget

I don't understand the difference, they both seem the same but I guess they are not.

Any examples of when to use one or the other would be appreciated.

like image 760
Artemix Avatar asked May 07 '11 13:05

Artemix


People also ask

What is the difference between E target and e currentTarget?

e. target is what triggers the event dispatcher to trigger and e. currentTarget is what you assigned your listener to.

What is the difference between event target and event currentTarget properties?

target is the root element that raised the event. currentTarget is the element handling the event.

What is e currentTarget?

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 means E target?

Thus e. target. value is the value property of some DOM element, in this case that means the text entered in the search input.


2 Answers

e.target is what triggers the event dispatcher to trigger and e.currentTarget is what you assigned your listener to.

like image 105
Ben Gale Avatar answered Sep 28 '22 05:09

Ben Gale


Ben is completely correct in his answer - so keep what he says in mind. What I'm about to tell you isn't a full explanation, but it's a very easy way to remember how e.target, e.currentTarget work in relation to mouse events and the display list:

e.target = The thing under the mouse (as ben says... the thing that triggers the event). e.currentTarget = The thing before the dot... (see below)

So if you have 10 buttons inside a clip with an instance name of "btns" and you do:

btns.addEventListener(MouseEvent.MOUSE_OVER, onOver); // btns = the thing before the dot of an addEventListener call function onOver(e:MouseEvent):void{   trace(e.target.name, e.currentTarget.name); } 

e.target will be one of the 10 buttons and e.currentTarget will always be the "btns" clip.

It's worth noting that if you changed the MouseEvent to a ROLL_OVER or set the property btns.mouseChildren to false, e.target and e.currentTarget will both always be "btns".

like image 24
Zevan Avatar answered Sep 28 '22 04:09

Zevan