Difference between event.target and event.currentTarget properties in an event object.

In Flex, an event object is created whenever an event is dispatched. The event object has two important properties – event.target and event.currentTarget. The event.target property references the event dispatcher while event.currentTarget property refers to the node which is currently being checked for event listeners. While event.target property stays the same throughout the event flow (capturing, targeting and bubbling phases), the event.currentTarget property changes on each node.

For example, if an event is fired from a TextInput component placed inside a Panel, event.target in this case will be TextInput throughout the event flow while event.currentTarget will vary from Application to Panel to TextInput as the event object goes up or down the flow chain.

An important point to note over here is that when an event is fired from a component, event.target property does not necessarily reference the component which dispatched the event. It may refer to a subcomponent which is part of that component. This means that if you click on a button’s label, the event object’s event.target property won’t be button (as you might have hoped) but will be a subcomponent (within the button class) – UITextField (renders label in a button).This is a very important point while handling events. In this case, if the button component was listening for the click event, the event handler will be called if you are using event.currentTarget property since (sooner or later) event.currentTarget will refer to the button component as the mouse event bubbles through the chain. But, if you were using event.target property, the event handler will never get called as a subcomponent (UITextField) fired the event and is referred to in event.target property. Hence, you should always use event.currentTarget property instead of event.target.

Advertisements
Tagged

18 thoughts on “Difference between event.target and event.currentTarget properties in an event object.

  1. Arun says:

    Thanks,

    It was very useful 🙂

  2. Niels says:

    Thank you, helped me a lot! 🙂

  3. krishna says:

    Thanks It is very Useful

  4. deep says:

    Thaks buddy ! it was really useful .

  5. Jeff says:

    you da man! Thanks.

  6. Randhir Kumar says:

    Thanks ,

    It was really useful.

  7. Tarique Islam says:

    Thanks
    It actually explained a lot

  8. Korean Flexer says:

    thanks..good explain..

  9. vasanth says:

    Thanks,
    Explanation is Good….

  10. Brian says:

    2 years working with flex and I still check this every few months:) Your point that “event.target property stays the same throughout the event flow” is the little reminder that will stay with me.

  11. wanting252 says:

    Those always confuse me:-(!

  12. mallesh says:

    Hi,
    This is really very helpful, I read “The difference between them many places and none could give clear idea”. But yours is really…..

    Thanks,
    Mallesh

  13. Go throught the evt.currentTarget example:

    You have to create a grid of 24 movieclips containing their unique names as in array and then paste the following code on first frame in as3.0:

    var list:Array = [
    s1,
    s2,
    s3,
    s4,
    s5,
    s6,
    s7,
    s8,
    s9,
    s10,
    s11,
    s12,
    s13,
    s14,
    s15,
    s16,
    s17,
    s18,
    s19,
    s20,
    s21,
    s22,
    s23,
    s24
    ];

    list.forEach(setupSquare);
    function setupSquare(square:MovieClip, index:int, array:Array):void {
    square.addEventListener(MouseEvent.CLICK, onSquareClick);
    }

    function onSquareClick(me:MouseEvent):void {
    trace(me.currentTarget.name);
    }

    for a real example you can explore my website:
    http://www.admecindia.co.in/

  14. yami says:

    Hi, How can i fire a mouse click event manually from target? The event Listener is attached to currentTarget.
    My requirement is i should fire a click event from target and later it should be automatically propagate to the listener.

  15. Rich says:

    I was stuck trying to figure out why my button wasn’t working for like 30mins. Glad I found your post, super helpful and solved the problem.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s