Skip to main content
Participant
Nov 8, 2023 5:26 AM UTC
Answered

Add the values of checkboxes on an Adobe Form

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;

This topic is closed to new replies. Start a new post to keep the conversation going.
Nesa Nurani
Nesa NuraniCorrect answer
Community Expert
Nov 8, 2023 11:30 AM UTC

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;

6 replies

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
Nov 8, 2023 11:30 AM UTC

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;

Translate
Participant
Nov 8, 2023 2:28 PM UTC

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.   

Translate
try67
Community Expert
Community Expert
Nov 8, 2023 9:09 AM UTC

Do you have fields named "150.00", "75.00", etc? Aren't those the actual values you want to use?

Translate
Participant
Nov 8, 2023 2:32 PM UTC

No, those are the values I want to use for the checkboxes which are selected.

Translate
try67
Community Expert
Community Expert
Nov 8, 2023 3:04 PM UTC

Yes, that's what I thought... Use Nesa's code from below, then.

Translate
Bernd Alheit
Community Expert
Community Expert
Nov 8, 2023 6:13 AM UTC

Check the JavaScript console for errors.

Translate