AS3: Difference between target vs currentTarget

13,016

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.

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.

Share:
13,016
Swati Singh
Author by

Swati Singh

Software Engineer | Blogger | Addicted to Learn New Technologies

Updated on June 25, 2022

Comments

  • Swati Singh
    Swati Singh almost 2 years

    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?

  • Swati Singh
    Swati Singh over 12 years
    i want a simple example in as3 for target and current target. please help
  • Swati Singh
    Swati Singh over 12 years
    why event.target traces "[object TextField]", event originated what does it means?
  • Pranav Hosangadi
    Pranav Hosangadi over 12 years
    You 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
    Pranav Hosangadi over 12 years
    In other words, TARGET dispatches the event originally, and it bubbles up until it finds a listener in CURRENTTARGET
  • user533
    user533 over 11 years
    Excellent answer...clear cut..