O'Reilly logo

Actionscript Cookbook by Joey Lott

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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");

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required