Skip to main content
Participant
May 26, 2021
Answered

How can I add value to text box with multiple checkbox's "checked"?

  • May 26, 2021
  • 3 replies
  • 3981 views

I have 3 different check boxes and 1 Text Field. 

I am trying to get the Text Field value to stay at 0 when none of the boxes are checked. When the checkbox "CB1" is checked I'd like the value to be 169. When checkbox "CB5" is checked I'd like the value to be 399. When "CB3" is checked I'd like the value to be 1099. Any idea how to do this? I am new to scripting! 

This topic has been closed for replies.
Correct answer Nesa Nurani

There is no need to do all of that, just go to checkbox properties under options tab change export value of CB1 to 1,CB2 to 2 and CB3 to 3, now go to text field calculate tab and select 'Value is the...' leave it as SUM+ and click on 'pick' and select CB1,CB2,CB3, thats it.

 

About your script:

Your first 3 conditions where you compare only one checkbox will always be active, for example:

else if(this.getField("CB1").valueAsString != "Off")
event.value = 1;

will overvrite:

else if(this.getField("CB1").valueAsString != "Off" && this.getField("CB2").valueAsString != "Off")
event.value = 3;

Because first condition is true, you can fix it by changing order of conditions.

 

3 replies

bricer67212240
Participant
April 12, 2022

Hello community,


This problem corresponded to my need and I thought I had solved my case by reading this topic.
But it doesn't solve everything.

 

With the explained case I can do the following operations:
Let's take the following example as a value:
CB1 = 1
CB2= 2
CB3 = 3

So with the check boxes I get :
0 if CB1, CB2 and CB3 are not checked
6 if CB1, CB2 and CB3 are checked
1 if only CB1 is checked
2 if only CB2 is checked
3 if only CB3 is checked

 

But if  I check CB1 and CB2 I get 1, whereas I would like to get 3.
If I check CB1 and CB3, I still get 1 when I would like to get 4.

Here is the code that gives me this result. I can't understand why it doesn't work... 

would you have an idea to solve my problem ?

 

 

if(this.getField("CB1").valueAsString != "Off" && this.getField("CB2").valueAsString != "Off" && this.getField("CB3").valueAsString != "Off")
event.value = 6;

else if(this.getField("CB1").valueAsString != "Off")
event.value = 1;
else if(this.getField("CB2").valueAsString != "Off")
event.value = 2;
else if(this.getField("CB3").valueAsString != "Off")
event.value = 3;

else if(this.getField("CB1").valueAsString != "Off" && this.getField("CB2").valueAsString != "Off")
event.value = 3;
else if(this.getField("CB1").valueAsString != "Off" && this.getField("CB3").valueAsString != "Off")
event.value = 4;
else if(this.getField("CB2").valueAsString != "Off" && this.getField("CB3").valueAsString != "Off")
event.value = 5;

else event.value = 0;

 

 

Thx for your help 

 

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
April 13, 2022

There is no need to do all of that, just go to checkbox properties under options tab change export value of CB1 to 1,CB2 to 2 and CB3 to 3, now go to text field calculate tab and select 'Value is the...' leave it as SUM+ and click on 'pick' and select CB1,CB2,CB3, thats it.

 

About your script:

Your first 3 conditions where you compare only one checkbox will always be active, for example:

else if(this.getField("CB1").valueAsString != "Off")
event.value = 1;

will overvrite:

else if(this.getField("CB1").valueAsString != "Off" && this.getField("CB2").valueAsString != "Off")
event.value = 3;

Because first condition is true, you can fix it by changing order of conditions.

 

bricer67212240
Participant
April 13, 2022

Thank you very much for your explanations and your help.
It's really great when it works when I was struggling with conditions for over an hour!

Thank you very much again

Nesa Nurani
Community Expert
Community Expert
May 26, 2021

As caclulation script of "Text Field" use this:

if(this.getField("CB1").valueAsString != "Off")
event.value = 169;
else if(this.getField("CB3").valueAsString != "Off")
event.value = 1099;
else if(this.getField("CB5").valueAsString != "Off")
event.value = 399;
else event.value = 0;

Brianna20Author
Participant
May 26, 2021

Thank you for your response. This script leaves the Text Field Value at 399. I need the value to change when different check boxes are marked! 

Inspiring
May 26, 2021

How do you use checkboxes, only one is checked or all 3 are checked, can you describe process?

 

Nancy OShea
Community Expert
Community Expert
May 26, 2021

[Moderator moved from Using the Community (forums) to Acrobat.]

 

Nancy O'Shea— Product User & Community Expert