1 Reply Latest reply on Jan 20, 2016 3:32 PM by George_Johnson

# Need Help: Need number of checkboxes selected to trigger content in text field

I am creating a form that has multiple checkboxes the user can select; let's call these checkboxes "cb1", "cb2", "cb3", "cb4", and "cb5". There is a text field (called "cbtotal") that I would like to say different things depending on how many checkboxes are selected. If any 2 checkboxes are selected, I want the text field to say "100". If any 3 checkboxes are selected, I want the text field to say "200". If all 5 checkboxes are selected, I want the text field to say "400". If only 1 checkbox is checked or if 4 checkboxes are checked, the text field can remain blank. How, if at all possible, do I make this all happen?

Also, can the resulting number (100, 200 or 400) in the "cbtotal" text field be used in a basic sum calculation later on? I want to be able to add that to other text fields in my form for a total at the bottom.

• ###### 1. Re: Need Help: Need number of checkboxes selected to trigger content in text field

The easiest way is to create a custom calculation script for the cbtotal field. It could look something like:

// Custom calculation script for text field

(function () {

// Initialize variables

var i, sum = 0;

// Loop throught the check boxes

// and count how many are selected

for (var i = 1; i <= 5; i += 1) {

if (getField("cb" + i !== "Off") {

sum += 1;

}

}

// Set this field's value based on how many check boxes are selected

switch (sum) {

case 2 :

event.value = 100;

break;

case 3 :

event.value = 200;

break;

case 5 :

event.value = 400;

break;

default:

event.value = "";  // 0, 1, or 4 check boxes selected

break;

}

})();

Yes, you can use this calculated field's value in subsequent calculations.