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

Checkbox Export Values and JavaScript

Community Beginner ,
Nov 08, 2023 Nov 08, 2023

Copy link to clipboard

Copied

Hello all, I've run into a JavaScript problem that I'm having trouble solving (and hopefully I can explain it properly).

 

I have a form that is calculating totals depending on which checkboxes are marked.

 

I've attached an image of an example of what I'm working on (it's not the actual form but a simplified version that I made in a spreadsheet).

 

I currently have the checkboxes set up to export a value so for example, if Checkbox A is marked, it exports a value of $50.

 

What I need help with is the Import Fee textbox. I need a script that takes the export value from each checkbox, and it triggers the Import Fee based on which checkbox is selected.

 

For example, if Checkbox A is marked, then the Import Fee will display $100. If no checkboxes are marked, the total needs to be $0.

 

Only one checkbox will be marked per form (but the form is not setup with radio buttons) so the import fees do not need to be added up.

 

I'm having trouble with getting the script to look for more than one export value from the checkboxes. I can make it work for one checkbox, but not multiple.

 

Any help is appreciated!

TOPICS
JavaScript

Views

396

Translate

Translate

Report

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 , Nov 14, 2023 Nov 14, 2023

It doesn't sound like the export value from the checkbox is really needed, you just need to know if it is checked?

 

In this case you could use a script like this.

 

if(this.getField("CB1").value != "Off")
   event.value = 425;
else if(this.getField("CB2").value != "Off")
   event.value = 410;
else
   event.value = 0;

    

However, you stated in the first post that only one checkbox can be checked. In that case the checkboxes should be setup to be mutually exclusive.  The script above uses the

...

Votes

Translate

Translate
Community Expert ,
Nov 08, 2023 Nov 08, 2023

Copy link to clipboard

Copied

Read this (the radio checkbox section in particular):

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

And this:

https://www.pdfscripting.com/public/How-to-Write-a-Basic-PDF-Calculation-Script.cfm

 

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

Votes

Translate

Translate

Report

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 ,
Nov 08, 2023 Nov 08, 2023

Copy link to clipboard

Copied

There are other solutions to this, for example, since only one checkbox will be checked and import fee is double that value just calculate "Import Fee" field to be Subtotal*2.

Votes

Translate

Translate

Report

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 ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

Sorry about that, I was a little unclear about the math. The totals above are an example. The actual math is more like Item A is $1471 and the import fee is $410, Item B is $425 and the import fee is $110, and the rest of the items and import fees vary.

 

I was able to get this JavaScript to work for one checkbox: 

event.value = this.getField("CB2").value == 1471 ? "410.00" : "";

 

But what I'm having trouble with, is writing event.values for more than one event. If I could just get an example of how to write it for two events, than I can figure out the rest.

Votes

Translate

Translate

Report

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 ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

So this is a calculation script for the "import fee" field?

How you set it up depends on the checkbox  configuration. Are the checkboxes mutually exclusive (i.e., only one can be checked), or can more then one checkbox be checked?  

 

 

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

Votes

Translate

Translate

Report

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 ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

Yes, this is the calcualtion script for the "import fee" field. The checkboxes are not mutually exclusive. 

Votes

Translate

Translate

Report

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 ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

It doesn't sound like the export value from the checkbox is really needed, you just need to know if it is checked?

 

In this case you could use a script like this.

 

if(this.getField("CB1").value != "Off")
   event.value = 425;
else if(this.getField("CB2").value != "Off")
   event.value = 410;
else
   event.value = 0;

    

However, you stated in the first post that only one checkbox can be checked. In that case the checkboxes should be setup to be mutually exclusive.  The script above uses the first checkbox that is checked and ignores the rest, regardless of how many are checked.

 

 

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

Votes

Translate

Translate

Report

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 ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

I've got the checkboxes set up to export a value that isn't On or Off. The current export value is a number. Will that matter?

 

Theortically, yes only one checkbox should be marked but I didn't set it up with radio buttons (for reasons, yay supervisors).

Votes

Translate

Translate

Report

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 ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

The value of a checkbox or radio button is the export value only if it is checked. The unchecked value is always "Off". The "if" statement in the code checks for the "Off" value because it will work regardless of the export value. 

Here are some articles on the topics:

https://www.pdfscripting.com/public/How-to-write-an-If-statement.cfm

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

 

 

 

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

Votes

Translate

Translate

Report

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 ,
Nov 14, 2023 Nov 14, 2023

Copy link to clipboard

Copied

LATEST

Fantastic. Thank you! I'll try to get it working today.

Votes

Translate

Translate

Report

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