4 Replies Latest reply on Feb 19, 2009 12:46 AM by Txoov

    Change button's click function

    Txoov
      How would I change a button's click event to call a different function?

      I have defined the button's click property like this:

      <mx:Button id="myButton" click="myFunction1()" />

      I need to use Actionscript to change the button's click event to call a different function. The below doesn't seem to work:

      myButton.addEventListener(MouseEvent.CLICK, myFunction2);
        • 1. Re: Change button's click function
          Richard_Abbott Level 3
          Hi Txoov,

          you need to do
          myButton.removeEventListener(MouseEvent.CLICK, myFunction1);
          before doing addEventListener.

          However, a quick test has found behaviour that doesn't seem quite right to me. If I declare as
          <mx:Button id="test_button" label="Test" click="test_click_1(event)" />
          and then in script do
          test_button.removeEventListener("click", test_click_1);
          test_button.addEventListener("click", test_click_2);
          (both test handlers simply put up an Alert)
          then I get both handlers still running, which I think is probably what you are describing in your post.

          If I declare as
          <mx:Button id="test_button" label="Test" />
          and then in a creation_complete event do
          test_button.addEventListener("click", test_click_1);
          followed by another handler that does
          test_button.removeEventListener("click", test_click_1);
          test_button.addEventListener("click", test_click_2);
          then all works as expected, ie just the 2 handler.

          So unless I am missing something obvious, then the final behaviour seems to depend on when and how you attached the handler to the button. Anyone have any thoughts on this?
          Richard
          • 2. Re: Change button's click function
            Level 7

            "Richard_Abbott" <webforumsuser@macromedia.com> wrote in message
            news:gne4p5$qm8$1@forums.macromedia.com...
            > Hi Txoov,
            >
            > you need to do
            > myButton.removeEventListener(MouseEvent.CLICK, myFunction1);
            > before doing addEventListener.
            >
            > However, a quick test has found behaviour that doesn't seem quite right to
            > me.
            > If I declare as
            > <mx:Button id="test_button" label="Test" click="test_click_1(event)" />
            > and then in script do
            > test_button.removeEventListener("click", test_click_1);
            > test_button.addEventListener("click", test_click_2);
            > (both test handlers simply put up an Alert)
            > then I get both handlers still running, which I think is probably what you
            > are
            > describing in your post.
            >
            > If I declare as
            > <mx:Button id="test_button" label="Test" />
            > and then in a creation_complete event do
            > test_button.addEventListener("click", test_click_1);
            > followed by another handler that does
            > test_button.removeEventListener("click", test_click_1);
            > test_button.addEventListener("click", test_click_2);
            > then all works as expected, ie just the 2 handler.
            >
            > So unless I am missing something obvious, then the final behaviour seems
            > to
            > depend on when and how you attached the handler to the button. Anyone have
            > any
            > thoughts on this?

            You normally can't remove event listeners added in MXML.


            • 3. Re: Change button's click function
              Richard_Abbott Level 3
              That's useful to know, thanks Amy. Normally I create most stuff dynamically anyway, but not always.

              Richard
              • 4. Re: Change button's click function
                Txoov Level 1
                Thanks, Richard and Amy. I decided not to add/remove the click event. Instead I use a static function when the button is clicked. Then in the static function a variable is checked to determine what function to run.

                <mx:Button id="myButton" click="myStaticFunction()" />

                private function myStaticFunction():void
                {
                if (strFunction == 'Function 1')
                myFunction1();
                else
                myFunction2();
                }