Skip to main content
Participant
November 8, 2023
Answered

Add the values of checkboxes on an Adobe Form

  • November 8, 2023
  • 3 replies
  • 633 views

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 has been closed for replies.
Correct answer Nesa Nurani

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;

3 replies

Nesa Nurani
Community Expert
Nesa NuraniCommunity ExpertCorrect answer
Community Expert
November 8, 2023

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;

Participant
November 8, 2023

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.   

try67
Community Expert
Community Expert
November 8, 2023

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

Participant
November 8, 2023

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

try67
Community Expert
Community Expert
November 8, 2023

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

Bernd Alheit
Community Expert
Community Expert
November 8, 2023

Check the JavaScript console for errors.