Stephen's dev blog

Flex getting selected RadioButton value

Posted on: 11 August, 2008

This is a pretty simple one but there are surprisingly few tutorials showing the user simply how to get a selected RadioButtonGroup’s value.

There are quite a number of tutorials covering change event triggers for RadioButton controls but this is slightly different.

Say you have a form and you want to process the RadioButton value after submitting the form instead of when the value of it changes.

To do this you actually have to create a RadioButtonGroup MXML tag for the radio buttons and select the value using the ‘selectedValue’ property of that RadioButtonGroup:

<mx:RadioButtonGroup id="myRadioButtonGroup" enabled="true" />
<mx:RadioButton label="I like RadioButtons" groupName="{myRadioButtonGroup}" left="505" top="64" selected="true" value="true"/>
<mx:RadioButton label="I don't like RadioButtons" groupName="{myRadioButtonGroup}" left="348" top="62" value="false"/>

Notice the {} around the groupName attributes of the RadioButton controls. You can then do this to select the value:

Alert.show(myRadioButtonGroup.selectedValue.toString());

Advertisements

14 Responses to "Flex getting selected RadioButton value"

How! Very nice tip! I’m adding a id to every RadioButton on my form and try every one. Thank’s.

Hi Carro,

Thanks for the comment. I was doing the same thing until I cam across this method. I hope it saves you time as it did me!

Stephen.

Hi Stephen,
I have a radiobuttongroup in a datagrid. I’m trying to the selected RadioButtonGroup for the selected row. I’ve dropped the RadioButtonGroup into my datagarid as an itemRenderer. Any ideas?

date = grdItems.???.rbgWeek.selectedValue;

Thanks!
Ryan

Hi Ryan,

Thanks for reading.

You’ll have to assign the selectedValue of the RadioButtonGroup to a public variable within the itemRenderer. You can then update this variable whenever the RadioButtonGroup changes.

I.E.

public var selectedRadioValue:Object;
...
<mx:RadioButtonGroup id="myGroup" itemClick="selectedRadioValue=myGroup.selectedValue" />

Not tested, let me know if it works.

Stephen.

Stephen,

Thanks for your response.

How do I reference selectedRadioValue in my ActionScript code?

When the user clicks a button, I need to grab the value of the radiobuttongroup and do some processing. I’m hitting my head against the wall. 🙂

Ryan

Hi Stephen,

I figured it out. Thanks for pointing me in the right direction. I ended up using this – change=”text=rbgWeek.selectedValue.toString(); outerDocument.doSomething(text)”

Ryan

Hi Ryan,

Sorry for the late reply! I’m glad you got it sorted out.

Stephen.

Would you know how to add that RadioButton Group to a datagrid? Id like to bind some data to a grid, and add a RBG to each row. Allowing the user to select some value for a large list.

thanks

Hi Ryan,

I have the same problem… and I am trying to do what you did. The problem is how will my RadioButtonHeaderRenderer will get the ‘outerDocument’ reference?

I am assuming ‘outerDocument’ is the DataGrid…

Hi Isan,

I think Ryan means that outerDocument is the parent Application.

Whether your RadioButtonHeaderRenderer will be able to reference the outerDocument I guess depends on where your RadioButtonHeaderRenderer is.

But in most cases I don’t think there should be a problem access functions of the parent application using outerDocument.function();

Let me know if you get it sorted!

Stephen.

Hi JK,

I’ve never actually tried that myself and after searching around, it doesn’t look like that many people have either!

I found a few results which looked hopeful but the site’s were down. The closest I could find was this blog post:

http://stackoverflow.com/questions/112036?sort=votes

Look at the second example, he made some changes from a comment’s suggestion and it seems he’s got it working.

Let me know how it goes, I’d be interested to see if you get it working!

Stephen.

Stephen,

Thanks for this summary. I did find a problem when I tried to implement it though. You say:

Notice the {} around the groupName attributes of the RadioButton controls. You can then do this to select the value:

But I found that the value was only returned if the groupName parameter was set to the id of the RadioButtonGroup WITHOUT the brackets.

I’m using Flex 3.

simply very nice!!Thanks!!!!!!!!

Thanks for the simple solution for my complex problem.

Comments are closed.


  • Patrick: I changed my code, but now I have another problem: Fatal error: Call to a member function isAvailable() on a non-object in /var/www/vhost/web/shopA
  • Stephen Gray: Hi Patrick, That first error is my fault. I had the method name for the refund() method as void() as I had copied the code from the other method!
  • Patrick: Hi Stephen, thanks for sharing your knowledge about creating a custom payment module. I need an extension for magento to handle a credit card payme

Categories

%d bloggers like this: