4 Replies Latest reply on Aug 13, 2009 2:20 AM by mfriasv

    Passing a value to a function from a radio button

    mfriasv Level 1

      Hi,

       

      I am sure this is very easy for you experts. I would like to pass the value of a selected radio button to a function:

       

      <mx:RadioButton id="jpg" label=".jpg" />
      <mx:RadioButton id="png" label=".png" />   
         
      <mx:Button label="Snapshot" click="clickHandler()" />

       

      How do I tell clickHandler to pass the value previously selected in the radio button?

       

      Regards,

      Manolo

        • 1. Re: Passing a value to a function from a radio button
          Kenny Yates

          First off you need to apply the Radio Butto Group then assign each Radio Button's groupName property to that Group

           

          <mx:RadioButtonGroup id="rbgTest" itemClick="handleSelection(event)"/>
          <mx:RadioButton id="rbOne" groupName="rbgTest" label="One" value="1"/>
          <mx:RadioButton id="rbTwo" groupName="rbgTest" label="Two" value="2"/>

           

          NOTE: Make sure to set the "value" property in each RadioButton as that is what is passed to your Function as the "selectedValue".

           

          Next you need to apply a Function that is called on the "itemClick" event of the RadioButtonGroup, NOT the RadioButtons themselves.

           

          private function handleSelection(event:ItemClickEvent):void
          {
              Alert.show("what is selected: " + event.currentTarget.selectedValue);
          }

           

          That should do it from a very elemental point of view.

          You can of course modify the items above for your specific requirments but this will get your started on the basics of using RadioButtons.

           

          HTH,

          Kenny Yates

          • 2. Re: Passing a value to a function from a radio button
            mfriasv Level 1

            Hi Kenny,

             

            Thanks for your answer! As it is now the user selects the radio button and immediately receives an alert message. What I would like to do is select a radio button and then click on a button placed below. This button would contain the value of the selected radio button. So two clicks - first select and then execute.

             

            How could this be done?

             

            Thanks,

            Manolo Frias

            • 3. Re: Passing a value to a function from a radio button
              Virat Patel Level 2

              Hello

               

              As u have mention that u need to first select the radio button and than button click.

               

              On button click event u can check that which radiobutton is selected and do as your requirement.

               

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="vertical">
                 
                  <mx:Script>
                      <![CDATA[
                          import mx.controls.Alert;
                         
                          private function onBtnClick(event:MouseEvent):void
                          {
                              if(rdUp.selected)
                              {
                                  Alert.show( rdUp.label + " Selected" )

                                  // do somthing
                              }
                              else if(rdDown.selected)
                              {
                                  Alert.show( rdDown.label + " Selected" )

                                  // do somthing

                              }
                              else
                              {
                                  Alert.show( " None of Selected" )

                                  // do somthing

                              }
                             
                          }
                      ]]>
                  </mx:Script>
                 
                  <mx:RadioButton id="rdUp" label="Up RadioButton" />
                  <mx:RadioButton id="rdDown" label="Down RadioButton" />
                 
                  <mx:Button label="Click" click="onBtnClick(event)" />
                 
              </mx:Application>

               

               

              WIth Regards

              Virat Patel

              • 4. Re: Passing a value to a function from a radio button
                mfriasv Level 1

                Thanks Virat!

                 

                That did the trick!

                 

                Manolo