1

In my program i am required to create buttons and their listeners dynamically. Is there a way to identify which button triggered the event. Each button does contain a unique text, but I tried using 'this' to access the text but not successful. Please help Code piece provided:

for(i=0;i<3;i++)
{
   subjectCode="MTOO"+(i+1);
   var subjectBtn:MovieClip=new subjectButton();
   subjectBtn.y=y+50+pos;
   subjectBtn.x=60;
   subjectBtn.subjCode.text=subjectCode;
   subjectBtn.addEventListener(MouseEvent.CLICK, displaySubjectAttendance);
   _subList.addChild(subjectBtn);
   pos+=140;
}
CloudyMarble
  • 36,908
  • 70
  • 97
  • 130
  • more needs explain your movieclip structure. and some more specifically your issue write. are you want to distinguish each button one eventlistenerHandler(displaySubjectAttendance)? – bitmapdata.com Feb 05 '13 at 13:35

2 Answers2

1
// This is how I will access the text in the event handler:

function displaySubjectAttendance( event:MouseEvent ):void {
    subjectButton( event.currentTarget ).subjCode.text
}
khailcs
  • 328
  • 1
  • 9
  • 2
    If you want to make sure you are referring to the button that registered the listener and not the text in the button, you should use event.currentTarget. – bwroga Feb 05 '13 at 13:46
  • Agreed bwroga, changed 'target' to 'currentTarget.' Tnx The_asMan but I checked this http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html#219548 – khailcs Feb 05 '13 at 14:01
  • I'm referring to the 4th paragraph of the "About the target and currentTarget properties" section: "The event.target property is set to the object that dispatched the event (in this case, UITextField), not the object that is being listened to (in most cases, you have a Button control listen for a click event)." – khailcs Feb 05 '13 at 14:06
  • @The_asMan: I did not vote the answer down. I tried to make that clear by asking why the question was voted down. I researched this before answering. Please see [Senocular's answer here](http://www.kirupa.com/forum/showthread.php?322613-Difference-between-target-and-currentTarget) and [Ben's answer here](http://stackoverflow.com/questions/5921413/difference-between-e-target-and-e-currenttarget) – bwroga Feb 05 '13 at 14:06
  • @khailcs, Thanks for your solution, it was precisely what I needed, Thanks again :) – user2043188 Feb 05 '13 at 14:59
1

You can access the button in your event listener as evt.currentTarget.

function displaySubjectAttendance(evt:MouseEvent):void {
    var button:MovieClip = evt.currentTarget as MovieClip;
}
bwroga
  • 5,379
  • 2
  • 23
  • 25