Skip to main content
Participant
March 2, 2025
Question

Need check box to check when 2 or more check boxes are checked

  • March 2, 2025
  • 1 reply
  • 262 views

I have a pdf form with many check boxes and I am trying to find a way to auto check a box based on the selection of other boxes.  For example, I have a question below and if 2 or more of the red text check boxes are marked (I used bullet points to act as a check box) I want it to check the box stating "Positive for 2 or more".  I have tired to find codes that would fill a text box with a color since the text box has the calculation options and I have tried to find a code for the run a javascript when Mouse Up, but I am struggling. I am also a beginner with the javascript/calculation stuff in PDF so please dumb it down. ANY help would be appreciated! 

 

How often do you feel tired or fatigued after you sleep?

  • Nearly every day
  • Three to four times a week
  • One to two times per week
  • One to two times per month
  • Never or nearly never

During your waking time, do you feel tired, fatigued, or not up to par?

 

  • Nearly every day
  • Three to four times a week
  • One to two times per week
  • One to two times per month
  • Never or nearly never

    Category 2 Score:
  • N/A (<2 positive responses)
  • Positive with 2 or more positive responses (in highlighted boxes)

1 reply

try67
Community Expert
Community Expert
March 2, 2025

Check-boxes don't have a calculation event, so you need to use a field that does, like a text field. It can be hidden, of course, but in this case we can use the text field where you want to change the text color based on the result. So if the check-boxes in question are called "Sleep1", "Sleep2", "Waking1", "Waking2" and the final two fields are called "Cat2Score" (the check-box with the result, with values of "Choice1" and "Choice2") and the (read-only) text field next to the second check-box is called "Cat2ScoreText", you can use this code as the custom calculation script of the latter:

 

 

var fields = ["Sleep1", "Sleep2", "Waking1", "Waking2"];
var total = 0;
for (var i in fields) {
	if (this.getField(fields[i]).valueAsString!="Off") total++;
}
if (total<2) {
	this.getField("Cat2Score").value = "Choice1";
	event.target.textColor = color.black;
} else {
	this.getField("Cat2Score").value = "Choice2";
	event.target.textColor = color.red;
}

 

Participant
March 4, 2025

THANK YOU so much for replying! I did as you said but it's not working. Do I need to put a value in the check boxes labeled Sleep 1 etc? I did have them at 1 and then Yes but nothing happened. The only thing that changed was the 'Choice2" box went black after I entered the calculation.  

 

This is what I made the choice boxes look like and then then the code I changed to match my boxes. I also named my fields what you put in your example and nothing happened. 

var fields = ["BQ1", "BQ2", "BQ3", "BQ4", "BQ5", "BQ6"];
var total = 0;
for (var i in fields) {
if (this.getField(fields[i]).valueAsString!="Off") total++;
}
if (total<2) {
this.getField("Cat1Score").value = "Choice1";
event.target.textColor = color.black;
} else {
this.getField("Cat1Score").value = "Choice2";
event.target.textColor = color.red;
}

 

THANK YOU again for your help!! 

Participant
March 4, 2025

I found this code and it is working by putting an X in the box, BUT it's only working if the 2 incidcated boxes are checked. I need it to  work if any 2 or more are checked and short of entering ALL possibilities, I am wondering if there is something that could check all the options without me spelling them out? ? 

var b1=this.getField("BQ1").isBoxChecked(0);
var b2=this.getField("BQ2").isBoxChecked(0);
var b3=this.getField("BQ3").isBoxChecked(0);
var b4=this.getField("BQ4").isBoxChecked(0);
var b5=this.getField("BQ5").isBoxChecked(0);
var b6=this.getField("BQ6").isBoxChecked(0);

if(b1&&b3&&!b2&&!b4&&!b5&&!b6)
event.value="X";
else if (b1&&b2&&!b3&&!b4&&!b5&&!b6)
event.value="X";
else if (b1&&b4&&!b3&&!b2&&!b5&&!b6)
event.value="X";
else if (b1&&b5&&!b3&&!b2&&!b4&&!b6)
event.value="X";
else if (b1&&b6&&!b3&&!b2&&!b5&&!b4)
event.value="X";
else if (b2&&b3&&!b1&&!b4&&!b5&&!b6)
event.value="X";
else if (b2&&b4&&!b1&&!b3&&!b5&&!b6)
event.value="X";
else if (b2&&b5&&!b1&&!b3&&!b4&&!b6)
event.value="X";
else if (b2&&b6&&!b1&&!b3&&!b5&&!b4)
event.value="X";
else if (b3&&b6&&!b1&&!b2&&!b5&&!b4)
event.value="X";
else if (b3&&b4&&!b1&&!b2&&!b5&&!b6)
event.value="X";
else if (b3&&b5&&!b1&&!b2&&!b6&&!b4)
event.value="X";
else if (b4&&b6&&!b1&&!b2&&!b5&&!b3)
event.value="X";
else if (b4&&b5&&!b1&&!b2&&!b6&&!b3)
event.value="X";
else if (b5&&b6&&!b1&&!b2&&!b4&&!b3)
event.value="X";
else event.value="";