Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Add the values of checkboxes on an Adobe Form

Community Beginner ,
Nov 07, 2023 Nov 07, 2023

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;

TOPICS
Acrobat SDK and JavaScript , Windows
549
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Nov 08, 2023 Nov 08, 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" },
    { chec
...
Translate
Community Expert ,
Nov 07, 2023 Nov 07, 2023

Check the JavaScript console for errors.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 08, 2023 Nov 08, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 08, 2023 Nov 08, 2023

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 08, 2023 Nov 08, 2023
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Nov 08, 2023 Nov 08, 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;

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Nov 08, 2023 Nov 08, 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.   

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines