Skip to main content
Known Participant
December 13, 2019
Answered

Checkbox conditioned upon value in another text field (or two fields)

  • December 13, 2019
  • 1 reply
  • 1767 views

How to make these two groups of checkboxes happen??  (note - my checkbox "group" is set up with same name but different choices 'Yes and No' so that they act more like radio buttons)  These two checkboxes are independant of each other.

CheckBox1 - Check Yes if A4 >= A5, else No

CheckBox2 - if A7 is "" then "", Check Yes if A7>=A3 or Amount7>=M, else No

 

Based upon other calcs you have helped me on, I've got my variables set up (I think) but how do I write the rest of the query?

 

var CB1 = this.getField("CheckBox1");  //  Yes or No;

var CB2 = this.getField("CheckBox2"); //  Yes or No;

var A1 = this.getField("Amount1").valueAsString; // amount ;        .......through A11

var M = this.getField("Minimum").valueAsString; // minimum amount;

{

     A3 = Number(A3);

     A4 = Number(A4);

     A5 = Number(A5);

     A7 = Number (A7);

     M = Number (M);

if (CB1 = ...............now what?

If it would be easier to change the checkboxes to radio buttons, I could.  But would prefer not to.

 

TIA ~ Michelle

 

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

After A7 is converted to a number it will not be an empty string, i.e. "".   Empty strings are converted to 0.

If CB2 is a checkbox then it does not make sense to set it to an empty string. I imagine you are doing this to uncheck the checkboxes, and it works because "" is not a valid value, so Acrobat converts it to "Off". What the code should do is set CB2.value = "Off";   This is important  because other PDF viewers are not obligated to convert invalid checkbox values to "Off". In all likely hood it's not a real problem, but by doing it correctly you are guarenteeing that it never will be a problem.  

 

You could also combine the cases where CB2 is set to "Yes".  

 

 if (A7 == 0) CB2.value = "Off";
 else if((A7 >= A3) || (A7 >= M)) CB2.value = "Yes";
 else CB2.value = "No";

 

Another variation is to combine both scripts into one calculation script. 

1 reply

Thom Parker
Community Expert
Community Expert
December 14, 2019

So, do you want the checkboxes to get checked automatically based on the entered values?

Where is this script? is it a calculation?

 

A checkbox is checked when it's value is the same as the Export value

Here's some simple code for checking your first box

if(A4 >= A5)
    CB1.value = "Yes";
else
    CB1.value = "No";
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
December 14, 2019

Thank you for your direction, Thom.  I created hidden fields to contain the calculations - CB1Decision and CB2Decision.  So far, it appears to be working perfectly.  But if you see an problem, let me know.  i've found in the past that what I thought was perfect was only working by default and undera scenario I hadn't tested it bombed!!

Again - thanks so much for your help!!!

 

for CB1Decision:

var CB1 = this.getField("CheckBox1");  //  Yes or No;
var A4 = this.getField("Amount4").valueAsString; // Policy amount;
var A5 = this.getField("Amount5").valueAsString; // Replacement cost ;

{
    A4 = Number(A4);
    A5 = Number(A5);
    if (A4=="" && A5=="") event.value = "";     

    else if (A4 >= A5) CB1.value = "Yes";
    else CB1.value = "No";
}

for CB2Decision:

var CB2 = this.getField("CheckBox2");  //  Yes or No;
var A3 = this.getField("Amount3").valueAsString; // Total Liens;
var A7 = this.getField("Amount7").valueAsString; // Replacement cost per unit;
var M = this.getField("Maximum").valueAsString; // Maximim NFIP policy per unit;

{
     A3 = Number(A3);
     A7 = Number(A7);
     M = Number(M);
    
     if (A7 == "") CB2.value = "";
     else if(A7 >= A3) CB2.value = "Yes";
     else if (A7 >= M) CB2.value = "Yes";
     else CB2.value = "No";
}

Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
December 14, 2019

After A7 is converted to a number it will not be an empty string, i.e. "".   Empty strings are converted to 0.

If CB2 is a checkbox then it does not make sense to set it to an empty string. I imagine you are doing this to uncheck the checkboxes, and it works because "" is not a valid value, so Acrobat converts it to "Off". What the code should do is set CB2.value = "Off";   This is important  because other PDF viewers are not obligated to convert invalid checkbox values to "Off". In all likely hood it's not a real problem, but by doing it correctly you are guarenteeing that it never will be a problem.  

 

You could also combine the cases where CB2 is set to "Yes".  

 

 if (A7 == 0) CB2.value = "Off";
 else if((A7 >= A3) || (A7 >= M)) CB2.value = "Yes";
 else CB2.value = "No";

 

Another variation is to combine both scripts into one calculation script. 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often