Recipe 11.11 Getting Checkbox Values

11.11.1 Problem

You want to determine the values (true or false) of checkboxes.

11.11.2 Solution

Use the getValue( ) method for each checkbox, or create a custom getValues( ) method for the checkbox group.

11.11.3 Discussion

The getValue( ) method returns true if a checkbox is checked and false if the checkbox is unchecked:

trace(myCheckBox0_ch.getValue(  ));

If you have created a checkbox group (see Recipe 11.10), you can add a custom method, getValues( ), that returns an array of the values for each checkbox in the group. The elements of the returned array contain three properties:

name

The name of the checkbox instance

label

The label value of the checkbox

value

The value of the checkbox (true or false)

Here is our custom getValues( ) method, which you should add to your Form.as file for easy inclusion in future projects:

// Create the new method, getValues(  ), for the FCheckBoxGroupClass. FCheckBoxGroupClass.prototype.getValues = function (  ) {   // The dataAr array is populated with the objects for each checkbox in the group.   var dataAr = new Array(  );   // Create two local variables for use in the for statement.   var cb, obj;   // Loop through every checkbox in the group.   for (var i = 0; i < this.radioInstances.length; i++) {     // cb refers to the current checkbox.     cb = this.radioInstances[i];     // For each checkbox, create an object with name, label, and value properties.      obj = new Object(  );     obj.name = cb._name;     obj.label = cb.getLabel(  );     obj.value = cb.getValue(  );     // Add the object to the array.     dataAr.push(obj);   }   // Return the array of values.   return dataAr; };

11.11.4 See Also

Recipe 11.9 and Recipe 11.10



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

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