Copy link to clipboard
Copied
I want to have a checkbox if based on the values from a combination of other checkboxes, ex; if 2 or more of 7 checkboxes are filled I want checkbox 8 to autofill. if only 1 of other 7 checkboxes are filled checkbox 8 does not autofill and is left blank
Copy link to clipboard
Copied
So you need to count the checked boxes. The best way to do this is to use a group naming convention for the first 7 checks, like this: "MyGroup.check1", "MyGroup.check2", etc.
Then this code will return true/false based on your criteria
var nCnt = 0;
this.getField("MyGroup").getArray().foreach(function(a){if(a.value == "Yes") nCnt++;});
this.getField("Check8").value = (nCnt>1)?"Yes":"Off";
Place this code in the MouseUp event of each of the 7 checkboxes.
Copy link to clipboard
Copied
I would do it using a (hidden) text field with the following calculation script (let's say the fields are named Check1 to Check8):
var ticked = 0;
for (var i=1; i<=7; i++) {
if (this.getField("Check"+i).valueAsString!="Off") ticked++;
}this.getField("Check8").tickThisBox(0, (ticked>=2));
The main benefit of doing it like that is that it will also work when the form is cleared, while the MouseUp approach won't.
Also, you have a single location with all the code necessary and you don't need to apply the same function to multiple fields.
Copy link to clipboard
Copied
True, but it also lowers performance because it runs whenever any field is changed, not just when it is needed. I try to avoid unnecessary calculation whenever possible.
Copy link to clipboard
Copied
That's true as well, although that's not a big deal if there aren't many calculated fields in the file.
Also, there are ways around it (by checking the event's source), while still allowing it to reset when the whole form is cleared.