Copy link to clipboard
Copied
I have an Adobe Acrobat form with seven (7) text boxes that contain numeric fields formatted as currency. These are the fees for each subject in a course.
Beside each fee there are two checkboxes that operate exclusively (like radio buttons). The Export Values for each of the two checkboxes are Enrol and RPL.
Here's a screenshot of part of the form...
The textbox field names are as follows...
E101-fee
E102-fee
E104-fee
E105-fee
E107-fee
F102-fee
F104-fee
At the bottom of the form is another currency text field named CourseFee (highlighted blue).
I need the course fee to sum the fees in the other seven text fields, but I only want to include those with the associated "Enrol" checkbox selected. If "RPL" is selected there will be no charge for that subject.
The "Enrol" and "RPL" checkboxes are automatically selected in specific configurations when exclusive checkboxes A, B, or C are selected (these are on another part of the form and are working perfectly). Clicking "A" selects all "Enrol" boxes, "B" selects "RPL" for 5 and "Enrol for 2, "C" selects "RPL" for 6 and "Enrol" for 1.
After this pre-selection occurs and the CourseTotal is displayed, any of the "Enrol" or "RPL" checkboxes can be subsequently changed and the CourseTotal is to update immediately.
Any help would be greatly appreciated.
1 Correct answer
You can use this code as the custom calculation script of your CourseFee field:
var fields = ["E101", "E102", "E104", "E105", "E107", "F102", "F104"];
var total = 0;
for (var i in fields) {
if (this.getField(fields).value=="Enrol") {
total+=Number(this.getField(fields+"-fee").value);
}
}
event.value = total;
Copy link to clipboard
Copied
What are the names of the check-box fields?
Copy link to clipboard
Copied
E101
E102
E104
E105
E107
F102
F104
Copy link to clipboard
Copied
You can use this code as the custom calculation script of your CourseFee field:
var fields = ["E101", "E102", "E104", "E105", "E107", "F102", "F104"];
var total = 0;
for (var i in fields) {
if (this.getField(fields).value=="Enrol") {
total+=Number(this.getField(fields+"-fee").value);
}
}
event.value = total;
Copy link to clipboard
Copied
Thanks. Really simple yet elegant code. 🙂
Copy link to clipboard
Copied
Your code is the simpliest and most elegant I have seen on these forums. I have a similiar problem but cannot get it to work. My checkboxes are Option1, Option2, Option3, etc to Option8. The fields with the values that need to be added are Option1-Cost, Option2-Cost, Option3-Cost, etc. The export value of the checkboxes is "Yes".
This is the code I created from your code but cannot get it to work. Can you help by chance?
var fields = ["Option1", "Option2", "Option3", "Option4", "Option5", "Option6", "Option7", "Option8"];
var total = 0;
for (var i in fields) {
if (this.getField(fields).value=="Yes") {
total+=Number(this.getField(fields+"-Cost").value);
}
}
event.value = total;
Copy link to clipboard
Copied
Thanks!
In your case it's even easier since you don't need to use an array for the field names because they are consistent.
Use this:
var total = 0;
for (var i=1; i<=8; i++) {
if (this.getField("Option"+i).valueAsString=="Yes") {
total+=Number(this.getField("Option"+i+"-Cost").valueAsString);
}
}
event.value = total;
Copy link to clipboard
Copied
This is awesome. Thank you so very much. Worked like a charm!
Copy link to clipboard
Copied
HI! I just tried this for myself and the code didn't work. Checkboxes are named "Course1", "Course2", "Course3", etc... Export value of checkboxes is "Yes". The field with values that need to be summed are "Course1-Credit", "Course2-Credit", etc...Some of the fields that need to be summed are static #s while a few are drop-down variables ranging from 0-4. Any help would be appreciated!!!
Copy link to clipboard
Copied
What does "it didn't work" mean, exactly? Did it produce no results? The wrong results? Was there an error message in the JS Console? Please be more specific.
Copy link to clipboard
Copied
Same here. I get no results whatsoever.
I used robs... field names just to ensure I have not made a mistake and still nothing. Am I doing something wrong?? Please help been at this for most of the day.
Copy link to clipboard
Copied
I've been trying to figure this out by reading all the other posts but I'm not getting anywhere. I want to add up all of my "Check Box" fields. I don't have (or know where it is) the "custom calculation script" box that I've seen on so many other posts. Am I in the wrong place all together?
Copy link to clipboard
Copied
The Calculate tab can be found under a text field, not under a check-box field.
Copy link to clipboard
Copied
Copy link to clipboard
Copied
@try67. I am trying to write code for when a checkbox is selected a fee will calculate based on Subtotal and Tax boxes multiplied by .035
So if Checbox7 is selected (Subtotal + Tax) * .035
Copy link to clipboard
Copied
And if it's not?
Copy link to clipboard
Copied
It is just 0 if not.
Copy link to clipboard
Copied
I should also clarify this is going into its own box labeled CC Fee.
Copy link to clipboard
Copied
You can use this code as the custom calculation script of that field:
event.value = (this.getField("Checkbox7").valueAsString=="Off") ? 0 : (Number(this.getField("Subtotal").valueAsString) + Number(this.getField("Tax").valueAsString)) * .035;
(I assume it's "Checkbox7", and not "Checbox7", as you wrote above)
Copy link to clipboard
Copied
That worked beautifully. Thank you
Copy link to clipboard
Copied
@try67Hoping you can help me figure this one out; I thought I had it, but it isn't working--I just continue to get a zero value regardless if the checkmark is "yes" or "no".
If the checkbox (circled in red) is checked, then I want it to add the coresponding value (circled in blue are hidden fields with default values matching the hours listed).
My checkboxes are named Core1, Core2, Core3, etc.; my hidden text fields are listed as CoreHours1, CoreHours2, CoreHours3, etc.
Below is a snip of my file as well as a snip of the code I'm trying to use.
Copy link to clipboard
Copied
Post the full code as text, or share the file with us, please.
Copy link to clipboard
Copied
File attached. Thank you!
Copy link to clipboard
Copied
You are very close, but the code for forming the number field name is incorrect. Also, this code won't work right now because there aren't enough number fields on the form.
Here's the corrected code:
var total = 0;
for (var i=1; i<=8; i++) {
if (this.getField("Core"+i).valueAsString=="Yes") {
total+=Number(this.getField("CoreHours" + i).valueAsString);
}
}
event.value = total;
Use the Acrobat JavaScript Reference early and often
Copy link to clipboard
Copied
That worked! Thank you for your help!!

