Recipe 11.9 Getting the Selected Radio Button Value

11.9.1 Problem

You want to get the value of the selected radio button from a group.

11.9.2 Solution

Use the getValue( ) method.

11.9.3 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.setData (212);
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.


11.9.4 See Also

Recipe 11.6, Recipe 11.7, and Recipe 11.11

    Part I: Local Recipes
    Part II: Remote Recipes