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());
#1 by Carro on 3 September, 2008 - 12:50 am
Quote
How! Very nice tip! I’m adding a id to every RadioButton on my form and try every one. Thank’s.
#2 by Stephen Gray on 3 September, 2008 - 9:30 am
Quote
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.
#3 by Ryan on 24 September, 2008 - 9:48 pm
Quote
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
#4 by Stephen Gray on 24 September, 2008 - 10:17 pm
Quote
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.
#5 by Ryan on 24 September, 2008 - 11:30 pm
Quote
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
#6 by Ryan on 25 September, 2008 - 12:10 am
Quote
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
#7 by Stephen Gray on 25 September, 2008 - 10:06 am
Quote
Hi Ryan,
Sorry for the late reply! I’m glad you got it sorted out.
Stephen.
#8 by JK on 10 November, 2008 - 6:00 pm
Quote
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
#9 by Isan on 14 November, 2008 - 6:59 am
Quote
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…
#10 by Stephen Gray on 21 November, 2008 - 11:10 am
Quote
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.
#11 by Stephen Gray on 21 November, 2008 - 11:23 am
Quote
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.
#12 by Christian Yates on 25 August, 2009 - 8:57 pm
Quote
Stephen,
Thanks for this summary. I did find a problem when I tried to implement it though. You say:
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.
#13 by suresh on 4 September, 2009 - 12:07 pm
Quote
simply very nice!!Thanks!!!!!!!!
#14 by Neethun on 9 September, 2009 - 8:00 am
Quote
Thanks for the simple solution for my complex problem.