Skip to main content
Participant
December 11, 2018
Answered

Set a checkbox based on values of other related checkboxes

  • December 11, 2018
  • 2 replies
  • 711 views

Basically I'm looking for a master / slave type of behavior.

I have four checkboxes, cb0, cb1, cb2, and cb3. I'd like to check cb0 if ANY of the others are checked and uncheck cb0 if ALL of the others are unchecked. I would think there is a simple solution here as it should simply read somthing like:

var f0 = getField("cb0");

var f1 = getField("cb1");

var f2 = getField("cb2");

var f3 = getField("cb3");

if (f1.value === "On" || f2.value === "On" || f3.value === "On") {

     f1.value = "On";

} else {

     f1.value = "Off";

}

Many thanks in advance!

This topic has been closed for replies.
Correct answer Thom Parker

The script probably doesn't work because "On" is not the export value for the checkboxes. 

Read about checkboxes here: Checkboxes and Radio buttons

The script is best used in a calculation script.  Any calculation script on the form. You could even create a hidden field just for this purpose. The idea is to capture any change in the checkbox values, including programatic changes. The MouseUp script will only work for user interaction. It's also spread out over 3 fields, whereas the calculation script is in one place.

2 replies

Thom Parker
Community Expert
Community Expert
December 11, 2018

This search provides a wealth of information on programming checkboxes, including exactly your question.

https://forums.adobe.com/search.jspa?q=checkbox

About your code. You didn't ask any questions about it. Did you write this code? Does it produce an errors? Where is this code placed?

If you'd like help, then these are all important.

1)  As Bernd states, it is f0 you want to set, not f1

2) Did you set all the checkbox export values to "On"?  Cause the default on value is "Yes".

3) The location of this code is very important. It should be placed in a calculation script, so all the check box clicks are captured.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
chrisdagsAuthor
Participant
December 12, 2018

I wrote the code on the fly but it hasn't worked for me. I thought I would put it in a Mouse Up action for all checkboxes other than cb0...? Is syntax an issue? Obviously I meant to set f0 and not f1. Sorry about that and thanks again for your feedback!

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
December 12, 2018

The script probably doesn't work because "On" is not the export value for the checkboxes. 

Read about checkboxes here: Checkboxes and Radio buttons

The script is best used in a calculation script.  Any calculation script on the form. You could even create a hidden field just for this purpose. The idea is to capture any change in the checkbox values, including programatic changes. The MouseUp script will only work for user interaction. It's also spread out over 3 fields, whereas the calculation script is in one place.

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Bernd Alheit
Community Expert
Community Expert
December 11, 2018

Why do you set the value of f1?

chrisdagsAuthor
Participant
December 12, 2018

I meant to set f0 instead. I'm not sure that my syntax is sound here. Thanks again!