Flylib.com

Books Software

 
 
 

Recipe 13.14. Retrieving Values from Checkboxes and Toggle Buttons


Recipe 13.14. Retrieving Values from Checkboxes and Toggle Buttons

Problem

You want to retrieve the value from a checkbox or a toggle button.

Solution

Use the instance's selected property.

Discussion

Checkboxes and buttons that toggle provide basically the same type of functional-itythey can be selected or deselected. Each of these two types of components has a property named selected that returns either true or false . In the case of a checkbox, the selected property is true if the checkbox is checked and false if it is unchecked. The button that toggles returns TRue if the button is pressed in and false otherwise . The following code retrieves the current state of a checkbox named cchOptIn and writes it to the Output panel when the listener object's click( ) method gets called:

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



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



Recipe 13.16. Retrieving Date Values

Problem

You want to retrieve a date from a date form control.

Solution

If you are using a date chooser or date field, use the instance's selectedDate property. Otherwise, use the FormController component to handle your form processing.

Discussion

The date chooser and date field components both have a selectedDate property that returns the date that the user has selected. The value that the property returns in either case is in the format of an ActionScript Date object. In most cases, you will want to retrieve the selected date only after users have indicated that they want to process the form data. Typically, that is done by clicking a submit button. The following code shows an example in which the selected date from a date chooser named cdcCalendar is retrieved and displayed when the user clicks a button:

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

If you are not using a date field or a date chooser, but rather a text input or input text field, you can simply retrieve the information the user has input via the text property as discussed in Recipe 13.11. However, as was mentioned in Recipe 13.9, verifying that the information represents a valid date requires some advanced ActionScript code. Therefore, unless you are very familiar with ActionScript, you will likely find it much simpler to use a FormController component to manage your form.

See also

Recipe 13.9, Recipe 13.17