Difference between e.target and e.currentTarget

Actionscript 3EventsEvent Handling

Actionscript 3 Problem Overview


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.

Actionscript 3 Solutions


Solution 1 - Actionscript 3

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

Solution 2 - Actionscript 3

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".

Solution 3 - Actionscript 3

I like visual answers.

enter image description here

When you click #btn, two event handlers get called and they output what you see in the picture.

Demo here: https://jsfiddle.net/ujhe1key/

Solution 4 - Actionscript 3

e.currentTarget is always the element the event is actually bound do. e.target is the element the event originated from, so e.target could be a child of e.currentTarget, or e.target could be === e.currentTarget, depending on how your markup is structured.

Solution 5 - Actionscript 3

It's worth noting that event.target can be useful, for example, for using a single listener to trigger different actions. Let's say you have the typical "menu" sprite with 10 buttons inside, so instead of doing:

menu.button1.addEventListener(MouseEvent.CLICK, doAction1);
menu.button2.addEventListener(MouseEvent.CLICK, doAction2);
etc...

You can simply do:

menu.addEventListener(MouseEvent.CLICK, doAction);

And trigger a different action within doAction(event) depending on the event.target (using it's name property, etc...)

Solution 6 - Actionscript 3

target  is the element that triggered the event (e.g., the user clicked on)

currenttarget is the element that the event listener is attached to.

Solution 7 - Actionscript 3

make an example:

var body = document.body,
    btn = document.getElementById( 'id' );
body.addEventListener( 'click', function( event ) {
    console.log( event.currentTarget === body );
    console.log( event.target === btn );
}, false );

when you click 'btn', and 'true' and 'true' will be appeared!

Solution 8 - Actionscript 3

e.currentTarget would always return the component onto which the event listener is added.

On the other hand, e.target can be the component itself or any direct child or grand child or grand-grand-child and so on who received the event. In other words, e.target returns the component which is on top in the Display List hierarchy and must be in the child hierarchy or the component itself.

One use can be when you have several Image in Canvas and you want to drag Images inside the component but Canvas. You can add a listener on Canvas and in that listener you can write the following code to make sure that Canvas wouldn't get dragged.

function dragImageOnly(e:MouseEvent):void
{
    if(e.target==e.currentTarget)
    {
        return;
     }
     else
     {
        Image(e.target).startDrag();
     }
}

Solution 9 - Actionscript 3

  • e.target is element, which you f.e. click
  • e.currentTarget is element with added event listener.

If you click on child element of button, its better to use currentTarget to detect buttons attributes, in CH its sometimes problem to use e.target.

Solution 10 - Actionscript 3

e.currentTarget is element(parent) where event is registered, e.target is node(children) where event is pointing to.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArtemixView Question on Stackoverflow
Solution 1 - Actionscript 3Ben GaleView Answer on Stackoverflow
Solution 2 - Actionscript 3ZevanView Answer on Stackoverflow
Solution 3 - Actionscript 3Maria Ines ParnisariView Answer on Stackoverflow
Solution 4 - Actionscript 3Alex KView Answer on Stackoverflow
Solution 5 - Actionscript 3CayView Answer on Stackoverflow
Solution 6 - Actionscript 3Muthukrishnan KandasamyView Answer on Stackoverflow
Solution 7 - Actionscript 3Yuan ZhaohaoView Answer on Stackoverflow
Solution 8 - Actionscript 3AsadView Answer on Stackoverflow
Solution 9 - Actionscript 3Marcel GJSView Answer on Stackoverflow
Solution 10 - Actionscript 3Samyak JainView Answer on Stackoverflow