Copy link to clipboard
Copied
I am trying to write a script that adds the values of 11 different fields on a PDF form where 11 checkboxes representing the 11 fields are checked and adds 200 to the final amount. When the checkbox is not checked, I do not add the value representing that checkbox. I have written the script below but it only displays $200.
// Define an array with the names of the 11 checkboxes and the corresponding fields
var checkboxFieldNames = [
{ checkbox: "AGT", field: "150.00" },
{ checkbox: "OneYA", field: "75.00" },
{ checkbox: "2YA", field: "112.00" },
{ checkbox: "CGT", field: "75.00" },
{ checkbox: "GTLateFee", field: "10.00" },
{ checkbox: "ChapterLateFee", field: "20.00" },
{ checkbox: "NHBF", field: "100.00" },
{ checkbox: "EFCB500", field: "500.00" },
{ checkbox: "EFCB250", field: "250.00" },
{ checkbox: "EFCB100", field: "100.00" },
{ checkbox: "EFCB50", field: "50.00" }
];
var total = 0;
// Loop through the checkboxes and add their corresponding field values
for (var i = 0; i < checkboxFieldNames.length; i++) {
var checkboxName = checkboxFieldNames[i].checkbox;
var fieldName = checkboxFieldNames[i].field;
if (this.getField(checkboxName).isBoxChecked(0)) {
var fieldValue = parseFloat(this.getField(fieldName).value) || 0;
total += fieldValue;
}
}
// Add 200 to the total
total += 200;
// Set the total value in a result field
this.getField("TAD").value = total;
Use like this:
var checkboxFieldNames = [
{ checkbox: "AGT", field: "150.00" },
{ checkbox: "OneYA", field: "75.00" },
{ checkbox: "2YA", field: "112.00" },
{ checkbox: "CGT", field: "75.00" },
{ checkbox: "GTLateFee", field: "10.00" },
{ checkbox: "ChapterLateFee", field: "20.00" },
{ checkbox: "NHBF", field: "100.00" },
{ checkbox: "EFCB500", field: "500.00" },
{ checkbox: "EFCB250", field: "250.00" },
{ checkbox: "EFCB100", field: "100.00" },
{ chec
...
Copy link to clipboard
Copied
Check the JavaScript console for errors.
Copy link to clipboard
Copied
Do you have fields named "150.00", "75.00", etc? Aren't those the actual values you want to use?
Copy link to clipboard
Copied
No, those are the values I want to use for the checkboxes which are selected.
Copy link to clipboard
Copied
Yes, that's what I thought... Use Nesa's code from below, then.
Copy link to clipboard
Copied
Use like this:
var checkboxFieldNames = [
{ checkbox: "AGT", field: "150.00" },
{ checkbox: "OneYA", field: "75.00" },
{ checkbox: "2YA", field: "112.00" },
{ checkbox: "CGT", field: "75.00" },
{ checkbox: "GTLateFee", field: "10.00" },
{ checkbox: "ChapterLateFee", field: "20.00" },
{ checkbox: "NHBF", field: "100.00" },
{ checkbox: "EFCB500", field: "500.00" },
{ checkbox: "EFCB250", field: "250.00" },
{ checkbox: "EFCB100", field: "100.00" },
{ checkbox: "EFCB50", field: "50.00" }
];
var total = 0;
for(var i in checkboxFieldNames){
if(this.getField(checkboxFieldNames[i].checkbox).valueAsString !== "Off")
total += Number(checkboxFieldNames[i].field);}
if(total != 0)total += 200;
this.getField("TAD").value = total;
I added so if no box is checked 200 is not added, if you still wish to add 200 when no box is checked remove this part "if(total != 0)".
Also, if you use script in the field where you want to show result, you can replace:
this.getField("TAD").value = total;
with:
event.value = total;
Copy link to clipboard
Copied
Thank you Mrs. Nurani,
That worked! I only had to make a slight adjustment to get the 200 value to remain constant.
Thank you to all who responded.
Find more inspiration, events, and resources on the new Adobe Community
Explore Now