Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3: Difference between target vs currentTarget [duplicate]

Possible Duplicate:
Difference between e.target and e.currentTarget

I don't really understand the difference between these two

event.target and              

event.CurrentTarget and explanation.

Can somebody explain this to me on a simple example?

like image 395
Swati Singh Avatar asked Jan 19 '23 01:01

Swati Singh


2 Answers

Suppose you create a TextInput object.

import fl.controls.TextInput;
import flash.events.MouseEvent;

var t:TextInput;

function init():void {
    t = new TextInput();
    t.x = 100;
    t.y = 100;
    t.width=100;
    t.height=30;
    t.addEventListener(MouseEvent.CLICK, fresult);
    this.addChild(t);
}

function fresult(e:Event):void {
    trace(e.target);
    trace(e.currentTarget);
}

init();

Clicking on the TextInput gives the trace of:

[object TextField]
[object TextInput]

This means:

event.target is the object from which the event originated. i.e. in this case, a TextField was clicked on, so the event originated from the TextField.

event.currentTarget is the object which called the listener. In this case, the TextInput called the listener, so the currentTarget is TextInput

like image 80
Pranav Hosangadi Avatar answered Feb 21 '23 12:02

Pranav Hosangadi


A link can be used to you is as follows.

Click

You can see that difference in UI by visit following link

Click

In theory,

Its actually kind of the opposite.

currentTarget is what you set the listener to. target is what causes the event (what calls dispatchEvent). Both currentTarget and target will be the same for non bubbling events. For bubbling events, they will be different when addEventListener is called on a parent for an event dispatched by a child. Then, currentTarget will be the parent and target will be the child (the child that actually dispatched the event).

currentTarget is usually what you want to specify in your event handlers because thats the object you added the listener to. You'll only need to reference target if you need to know the source child of that object where the event actually originated.

like image 27
Sagar Rawal Avatar answered Feb 21 '23 13:02

Sagar Rawal