2 Replies Latest reply on Mar 16, 2015 9:26 AM by jfillman

    Identify What was Clicked

    jfillman Level 1

      I have a click event listener on a group component. I'm trying to figure out if a label was clicked or anywhere else in the group was clicked. The event.currentTarget is just returning the group in all instances.

       

      I originally had a click event set on the label and a click event on the group, but I end up with two events. Since the click event on the label is designed to remove the Group from display, remove event listeners, etc., the second click even then throws an error, because the Group no longer exists.

       

      The code below should give you a good idea as to what I am trying to do. Currently the groupClick() function will throw an error. I typed in in this way to explain what I am trying to accomplish.

       

      <?xml version="1.0" encoding="utf-8"?>
      <!-- Specify the root tag and namespace. -->
      <s:Group xmlns:fx="http://ns.adobe.com/mxml/2009"
          xmlns:s="library://ns.adobe.com/flex/spark"
          xmlns:mx="library://ns.adobe.com/flex/mx"
      height="200" width="200" click="groupClick(event)">
      <fx:Script>
        <![CDATA[

       


         import flash.events.MouseEvent;

       

         private function groupClick(event:MouseEvent):void {

                     // Do this if clicking anywhere that is NOT on the closeX label

                     this.parentDocument.openGroupList.selectedIndex = this.parentDocument.openGroupListAC.getItemIndex(this as Group);

                     this.parentDocument.openGroupListClick();

       

                     // Do this if clicking on the closeX label

                     this.removeEventListener(MouseEvent.MOUSE_MOVE, this.parentDocument.mouseMoveHandler);

                     this.parentDocument.openGroupListAC.removeItemAt(this.parentDocument.openGroupListAC.getI temIndex(this as Group));  

         }
         
        
         ]]>
        </fx:Script>

       

      <s:Label top="7" right="7" text="X" id="closeX" fontSize="14" color="0xffffff" buttonMode="true" useHandCursor="true"/>

      </s:Group>

       

      I could use:

       

      if (String(event.target).indexOf('closeX') > 0) {

                     this.removeEventListener(MouseEvent.MOUSE_MOVE, this.parentDocument.mouseMoveHandler);

                     this.parentDocument.openGroupListAC.removeItemAt(this.parentDocument.openGroupListAC.getI temIndex(this as Group));

      }

      else { ....

       

      That works, but string comparisons seem rather inefficient. If someone has a better way, I'd like to know!!

       

      Message was edited by: JFillman