11.11. Getting Checkbox Values

Problem

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

Solution

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

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 ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.