Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

check box equals a value in form when checked

Community Beginner ,
Jun 03, 2020 Jun 03, 2020

I am creating a form that adds amounts for a total. I want to create a check box (BOX1), that when checked, puts a specific dollar amount in another text box  (#4) which is then added to other amounts to create a total for purchase. 

TOPICS
Acrobat SDK and JavaScript
11.4K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Jun 03, 2020 Jun 03, 2020

You mean that the script is on a MouseUp "run a JavaScript" on the Box1 checkbox?

Ok, then the script should look like this:

 

if (event.target.isBoxChecked(0))
      this.getField("BOX4").value = 4; 
else
      this.getField("BOX4").value = 0; 

 

 

Translate
Community Expert ,
Jun 03, 2020 Jun 03, 2020

Hi,

Something like this should work for the checkbox part :

if (this.getField("BOX1").isBoxChecked(0)){
this.getField("BOX4").value = 4; // dollar amount
}

 

Then you can just calculate the fields as normal,

 

see here for more information - https://community.adobe.com/t5/acrobat/field-calculation/td-p/10314382?page=1

 

Regards

Malcolm

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 03, 2020 Jun 03, 2020

That works great.. thanks... however, if i 'uncheck' the box the amount stays in Box4.... do i need another script that says if uncheck, value is '0' ?

 

where does one find these scripts or how to make them? is there a list somewhere?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2020 Jun 03, 2020

You didn't mention any other cases in your original post. But that's easy enough to fix. Just add an "else" to the current code. 

Also, where is this script? If it is in the custom calculation script for the "BOX4" field, then it needs to be written like this:

if (this.getField("BOX1").isBoxChecked(0))
      event.value = 4; 
else
      event.value = 0; 

 

It is very important that "event.value" is used to set the field value because this value is used in a following calculation, but more importantly, this is the correct way to handle data in calculation script. Accessing the field value external to the script can cause all kinds of problems.  

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 03, 2020 Jun 03, 2020

right now the script is in the actions/run javascript in Box1 and the data in box4 is read only.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jun 03, 2020 Jun 03, 2020

You mean that the script is on a MouseUp "run a JavaScript" on the Box1 checkbox?

Ok, then the script should look like this:

 

if (event.target.isBoxChecked(0))
      this.getField("BOX4").value = 4; 
else
      this.getField("BOX4").value = 0; 

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jun 03, 2020 Jun 03, 2020

That did the trick... thanks.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 28, 2022 Oct 28, 2022

Good afternoon, I have a small pdf trying to work some calculations for membership options and values.  I have tried the script below and was able for the amount to show up correctly, however when I click off the same box the value does not change and the check mark seems to not work consistently. 

 

if (event.target.isBoxChecked(0))
      this.getField("BOX4").value = 4; 
else
      this.getField("BOX4").value = 0

 Full disclosure my skills at java are minimal but will try to figure it out with some guidance.

Thanks

Doug

if (event.target.isBoxChecked(0))
      this.getField("BOX4").value = 4; 
else
      this.getField("BOX4").value = 0

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 31, 2022 Oct 31, 2022

It looks like you want the amounts associated with all check boxes added up? If this is true, then the solution provided on this thread is not suitable.

 

Instead, set the export value of all the check boxes to the associated amount. Then use the sum function on the Total field to add the checkboxes.  And be sure to delete all of the MouseUp scripts. This is critical. Get rid of those scripts. 

 

You can read more about checkboxes on PDF here:

https://www.pdfscripting.com/public/Checkboxes-and-Radio-Buttons.cfm

 

 

 

 

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Nov 05, 2022 Nov 05, 2022

ok Thanks for the note and the guidance, Let me check things out.  IM a novice when it comes to javascripting.  One thing I did neglect to add is the last 3 are like add ons to the selections above one or more apply.  The top selections are pick one and the bottom 3 are potential ad ons. Let me see how I do. Thanks Doug

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Jan 03, 2023 Jan 03, 2023

What would be the code in this last case?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 05, 2023 Dec 05, 2023

Hi, I'm trying to use nested if statements so that if they check one of 5 checkboxes the value is displayed as the total. It seems that one i get beyond 3 of the if statements, then it works for only the first and last if statement.

if (this.getField("1.0").isBoxChecked(0))
event.value = 0;
if (this.getField("1.1").isBoxChecked(0))
event.value = 1;
if (this.getField("1.2").isBoxChecked(0))
event.value = 2;
if (this.getField("1.3").isBoxChecked(0))
event.value = 3;
else
event.value = 0;

 

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2023 Dec 05, 2023

Add else before the last 3 if

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 05, 2023 Dec 05, 2023

The way you set it up you can check all the checkboxes, if only one checkbox should be selected instead, give all checkboxes same name
for example: Q1 and give each checkbox export value to be the score, 1st checkbox give export value 0, 2nd give export value 1, 3rd give export value 2...etc
this way only one checkbox can be checked at the time.
Then in total field use this:
if(this.getField("Q1").valueAsString !== "Off")
event.value = this.getField("Q1").value;
else
event.value = 0;

If you name all your rows in order (Q1,Q2,Q3...etc) you could also use a loop to calculate all the totals with one script.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 05, 2023 Dec 05, 2023
LATEST
0 is equal to 1. "nullptr" is some evaluations. 0.5 is equal to 5 and
similiar to 2 and 3 multiplied by 1, then 7 and 10. 9 Read with light
purple compressed.*3√✓
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines