Skip navigation
CarrionMisery
Currently Being Moderated

Multiple Check Boxes Update Text Box

May 11, 2012 1:23 PM

I have 7 check boxes next to 7 text boxes. The text boxes read:

  1. Red
  2. Blue
  3. Green
  4. Cyan
  5. Magenta
  6. Yellow
  7. Black

 

I have an 8th text box that is supposed to display the check boxes that are selected. So if 1, 3, 5 and 7 are checked, the 8th box should say:

 

Red Green Magenta Black

 

I already found a way to make this work, but the length of code is insane. I have to make an if, then statement for every single possible combination:

 

else if((redcheck == "Yes") && (bluecheck != "Yes") && (greencheck == "Yes") && (cyancheck != "Yes") && (magentacheck == "Yes") && (yellowcheck != "Yes") && (blackcheck == "Yes"))

            event.value = this.getField("red").value + this.getField("green").value + this.getField("magenta").value + this.getField("black").value;

 

This is brain numbing and I keep losing my place of what else needs to be combined. I have a feeling there is a more simple way to accomplish this. Any help will be appreciated.

 
Replies
  • Currently Being Moderated
    May 11, 2012 2:10 PM   in reply to CarrionMisery

    I would set the export value of each check box to the color for that check box. Then for the script, I would load the value of each check box that does not have a value of "Off" into an array. And then I can can set the field value equal to the contents of the "joined" array.

     

    // array of field name to process

    var aFieldNames = new Array("CBColor.0", "CBColor.1", "CBColor.2", "CBColor.3", "CBColor.4", "CBColor.5", "CBColor.6");

    // variable for value of an individual field

    var cColor = "";

    // array of values from fields with a value not equal to "Off"

    var aColors = new Array();

    // process each field in field name array

    for (i = 0; i < aFieldNames.length; i++) {

    // get the value from a field

    cColor = this.getField(aFieldNames[i]).value;

    // add value to array of colors only if value not equal to "Off"

    if(cColor != "Off") {

    aColors[aColors.length] = cColor;

    } // end field value not "Off"

    } // end loop field name processing

    // set value to the values in the color array

    event.value = aColors.join(", ");

     

    Change the array of field names as needed.

     
    |
    Mark as:
  • Currently Being Moderated
    May 11, 2012 2:57 PM   in reply to CarrionMisery

    The field name are in the array aFieldNames. If you want to change the values, then change the export value of the check box. There are comments within the script to help explain what is being done.

     

    You could add a script to each text field to change the "exportValues" of the associated check box based on the value of the text field so as a text field is changed, the check box' export value gets changed.

     

    You should also allow for the clearing of the from and not losing your options data data.

     
    |
    Mark as:
  • Currently Being Moderated
    May 12, 2012 6:49 AM   in reply to CarrionMisery

    OK.

     

    Try this script and change the field names as necessary:

     

    // define array of pairs of check box and associated text fields

    // each primary element of the array consist of the check box field and the text field for that check box

    // field names are an array of 2 items the first item the check box field name and the second the text field name

    var aFields = new Array(

    ["CBOption.0", "OptionText.0"],

    ["CBOption.1", "OptionText.1"],

    ["CBOption.2", "OptionText.2"],

    ["CBOption.3", "OptionText.3"],

    ["CBOption.4", "OptionText.4"],

    ["CBOption.5", "OptionText.5"],

    ["CBOption.6", "OptionText.6"]

    );

    console.show();

    console.clear();

    console.println("Number of fields: " + aFields.length);

    // define array for values of text for selected fields

    var aValues = new Array();

    // process the fields in the aFields array

    for ( i = 0; i < aFields.length; i++) {

    console.println("Check box: " + aFields[i][0] + " Text field:" + aFields[i][1]);

    console.println("Check box value: " + this.getField(aFields[i][0]).value);

    // see if check box field i is not off

    if(this.getField(aFields[i][0]).value != "Off") {

    console.println("Text field value: " + this.getField(aFields[i][1]).value);

    // check box is selected - add value of associated text field

    aValues[aValues.length] = this.getField(aFields[i][1]).value;

    } // end check box not off

    } // end loop for aField element

    // set field value to contents of aValues array

    event.value = aValues.join("; ");

     

    It will even show you the field names and values as it runs.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 5, 2012 2:14 PM   in reply to CarrionMisery

    You need to set each Field where you placing the value tot he correct format. The format with currency is a visible only property, the raw number without the currency symbol is used in calculations. and it is assumed the creator is keeping the currency of the data consistent.

     

    Sort of like computing rocket paths with miles and kilometers, one needs to keep track of the unit of measurement and not mix them in the same calculation.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points