11.9. Getting the Selected Radio Button Value
Problem
You want to get the value of the selected radio button from a group.
Solution
Use the getValue( )
method.
Discussion
First, create two radio buttons and assign them to the same group (named “answerGroup”):
_root.attachMovie("FRadioButtonSymbol", "answer0_rb", 1); _root.attachMovie("FRadioButtonSymbol", "answer1_rb", 2); answer0_rb.setGroupName("answerGroup"); answer1_rb.setGroupName("answerGroup"); answer0_rb.setLabel("Hot"); answer0_rb.setData (212); answer1_rb.setLabel("Cold"); answer1_rb.setData (32); answerGroup.setPositions( );
Some time later, you can obtain the data value of the selected radio
button by invoking the getValue( )
method on the
radio button group. The radio button group is created automatically
when a radio button is added to a group using setGroupName(
)
. In this example, the return value is either 212 or 32,
not the string “Hot” or
“Cold”. That is,
getValue( )
returns the selected
button’s data
property, not its
label
property.
selectedVal = answerGroup.getValue( );
As with getting the selected values for other UI components, you typically get the values for radio button groups once the user clicks on the Submit button:
function onClick ( ) { var selectedVal = answerGroup.getValue( ); // Do something with data here. } submitBtn.setClickHandler("onClick");
See Also
Get Actionscript Cookbook now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.