Skip to main content
Participant
November 8, 2023
Answered

Checkbox Export Values and JavaScript

  • November 8, 2023
  • 2 replies
  • 3159 views

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!

Correct answer Thom Parker

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).


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

 

 

 

2 replies

Nesa Nurani
Community Expert
Community Expert
November 9, 2023

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.

PNW_BBAuthor
Participant
November 14, 2023

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.

Thom Parker
Community Expert
Community Expert
November 14, 2023

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 PDFScriptingUse the Acrobat JavaScript Reference early and often
Thom Parker
Community Expert
Community Expert
November 9, 2023
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often