AS3: Difference between target vs currentTarget
Solution 1
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
Solution 2
A link can be used to you is as follows.
You can see that difference in UI by visit following link
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.
Swati Singh
Software Engineer | Blogger | Addicted to Learn New Technologies
Updated on June 25, 2022Comments
-
Swati Singh almost 2 years
Possible Duplicate:
Difference between e.target and e.currentTargetI 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?
-
Swati Singh over 12 yearsi want a simple example in as3 for target and current target. please help
-
Swati Singh over 12 yearswhy event.target traces "[object TextField]", event originated what does it means?
-
Pranav Hosangadi over 12 yearsYou might notice that
TextField
is the actual component in which you write text. TextInput is a component which CONTAINS a textField, and adds certain methods. So, when you are clicking, you are actually clicking on a textFIELD. This is the TARGET. But, suppose you don't know what happens behind the scenes, you wouldn't know that a TextField is the component which catches the click event. You would still add an event listener to the TextINPUT component. So the component to which you add the listener is in the currentTarget. Is this clear enough? -
Pranav Hosangadi over 12 yearsIn other words, TARGET dispatches the event originally, and it bubbles up until it finds a listener in CURRENTTARGET
-
user533 over 11 yearsExcellent answer...clear cut..