4

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?

Community
  • 1
  • 1
Swati Singh
  • 1,863
  • 3
  • 19
  • 40

2 Answers2

6

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

Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • 1
    why event.target traces "[object TextField]", event originated what does it means? – Swati Singh Dec 15 '11 at 09:33
  • 1
    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 Dec 15 '11 at 09:37
  • In other words, TARGET dispatches the event originally, and it bubbles up until it finds a listener in CURRENTTARGET – Pranav Hosangadi Dec 15 '11 at 09:39
4

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.

Community
  • 1
  • 1
Sagar Rawal
  • 1,442
  • 2
  • 12
  • 39