Recipe 13.15. Retrieving Values from Radio Button Groups


Problem

You want to retrieve a value from a group of radio buttons.

Solution

Use the radio button group's selectedData property.

Discussion

As discussed in Recipe 13.5, radio buttons are typically used in groups. Therefore, you usually will want to retrieve the value from the selected radio button in the group. In order to do that, all you need to do is use the selectedData property of the radio button group.

Recall that when you create a radio button instance, you set the groupName parameter. The groupName parameter defines how the radio buttons are associated. Not only that, but it also creates a new ActionScript object with that same name. For example, if you create five radio buttons and assign each of them the group name of rbgQuizQuestion, Flash will automatically create an ActionScript object named rbgQuizQuestion. You can later use that ActionScript object to retrieve the value that has been selected for the entire group of five radio buttons. That object has a property named selectedData that will return the chosen value.

If you have defined a data parameter value for the radio buttons, the radio button group's selectedData property will return the data value for the selected radio button. However, if you have not defined the data parameter value for the radio buttons, the selectedData property will simply return the chosen radio button's label value.

In most scenarios, you will want to retrieve the selected radio button value only after the user has clicked a submit button or some other similar event indicating that the form data should be processed. The following code is an example in which the selected value from a radio button group named rbgQuizQuestion is displayed in the Output panel when the user clicks on a button:

 var oListener:Object = new Object(); oListener.click = function(oEvent:Object):Void {   trace(rbgQuizQuestion.selectedData); }; cbtSubmit.addEventListener("click", oListener); 

See Also

Recipe 13.5




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net