Skip to main content
sasegars58
Known Participant
September 23, 2017
Answered

Calculation resulting from checkbox selection in pdf

  • September 23, 2017
  • 5 replies
  • 25454 views

I'm probably way out of my league here, but here's what I need to happen:

I have a fillable pdf with several checkboxes that would result in an amount being added to a total field (Ex: Full page ad / Exhibitor fee is $500 / Non exhibitor fee is $750), so the user selects two items (or none). If they check "Full page ad" checkbox, then they select whether or not they are an exhibitor, then the amount ($500) would go to the total field = $500. And several more optional add choices, etc.

So, I guess my question(s) are:

If a checkbox is selected, can a number populate in a separate field? And if so, how?

I am a javascript beginner, have only completed the calculations that were simple.

Many thanks in advance for help/advice.

Correct answer try67

They can only choose one box (either 500 or 700)


Then use this:

event.value = 0;

if (this.getField("fullpageexhibitor").value!="Off") event.value=500;

else if (this.getField("fullpageadregular").value!="Off") event.value=700;

5 replies

New Participant
December 24, 2023

// Add this script to the "Custom Calculation Script" of the total field

var total = 0;

// Function to update total based on checkbox selections
function updateTotal() {
total = 0;

// Check each checkbox and update total accordingly
if (this.getField("fullPageAd").value === "Yes") {
if (this.getField("exhibitor").value === "Yes") {
total += 500; // Full page ad exhibitor fee
} else {
total += 750; // Full page ad non-exhibitor fee
}
}

// Add more conditions for other checkboxes if needed

// Update the total field
this.getField("totalField").value = total;
}

// Set up event listeners for each checkbox
this.getField("fullPageAd").setAction("MouseUp", updateTotal);
this.getField("exhibitor").setAction("MouseUp", updateTotal);

// Add more event listeners for other checkboxes as needed

Participating Frequently
December 23, 2023

I am trying to do something similar but I think I am missing something.

Mine seems like it would be basic.

Text 1 = Text 2 +(5 if Checkbox 1 is checked) + (5 if Checkbox 2 is checked) + Text 3 + Text 4

PDF Automation Station
Community Expert
December 23, 2023

Is that the script you are using?

Participating Frequently
December 23, 2023

No, that is the end result I want to create. I am very new to editing Adobe but want to start making more use of this program I am paying for.

New Participant
September 21, 2019

Hi there

 

I've been searching the community and this thread is the closest I found regarding what I'm trying to achieve. I am very rusty with my script knowledge as I haven't practiced it for years; I'm practically beginner level at this point. I have a similar situation where I have a list of services (with checkboxes) that have the same pricing; however, I do want multiple selections added up to a total. 

 

The form allows the client to choose from 3 Service Package deals (Packages 1 to 3). Currently I have these as radio buttons where they can only select one. For the sake of math I'll keep the pricing simple: Packages 1 to 3 are $5000, $3000, and $1000 respectively. Each service package offers discounts on additional services. Package1 offers $1000 discount, Package2 $500, and Package3 no discount. The group of radio buttons is named "Group1" and their button choices are 1, 2, & 3 respectively.

 

There are 5 additional services, Services A to E. The client filling up the form can choose to select as many services, including none. Each service costs $500. However, depending on the Package deal they selected earlier in the form (Packages 1 to 3), they get 2 ($1000) or 1 ($500) services discounted. I would like to autofill the text field named "TotalExtras" so the client would instantly know how much extra on top of the base Package they have chosen.

 

First Example:

Client selects Package2 for $3000 (which will discount $500 from total extras). The client continues to next section and selects three additional services, say A, B & D. So the three choices should total $1500 ($500 each). But because they have chosen Package2, the autofill total in the TotalExtras text field should read "$1000" ($1500 additional services minus $500 Package2 discount). 

 

Second Example:

Client selects Package1 for $5000 (which will discount $1000 from total extras). The client continues to next section and selects only one additional service, they choose C. The single choice should total $500. But because they have chosen Package1, the autofill total in the TotalExtras text field should read "$0" ($500 additional services minus $1000 Package1 discount). The total for this TotalExtras field should never be negative and there is no option to go lower than the base price of the chosen Package.

 

Is this posssible? Or am I asking for too much? Also, am I supposed to have all the checkboxes under one name? They all currently have separate names.

 

Thanks in advance!

New Participant
June 8, 2020

I have a similar issue... this is the code im trying to use, Im trying to get a check box in adobe to (When checked) to subtract "TTLCalculated" And use that value in the next part of my code, wanted to know if what im doing wrong, im a newbie to javascript, would really appreciate the help!

 

var DA = Number(this.getField("Downpayment*").valueAsString);

var DA2 = Number(this.getField("TTLCalculated").valueAsString);

event.value = (DA-DA2);

if (this.getField("TTLCalculated").value!="Off") event.value=(DA-DA2);

else if (this.getField("TTLCalculated").value!="Off") event.value=0;

Thom Parker
Community Expert
June 8, 2020

YOu have exactly the same condition in both parts of the if block. Remove the second if. Just use an else.

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
sw777
Known Participant
July 7, 2019

I'm trying to do a similar thing but I'm having trouble adapting the following code to my situation...

event.value = 0;

if (this.getField("CheckBox1#0").value!="Off") event.value=120;

else if (this.getField("CheckBox1#1").value!="Off") event.value=450;

I have a suspicion it's because I'm using multiple checkboxes that have the same name but with different export values (to simulate radio button operation) and the checkbox name has a # in it.

Am I able to replace the checkbox name "CheckBox1#0" with the export value instead?

Also, to make matters worse I have 5 checkboxes that can all be different values (although 2 are the same) and I need the textbox to display the value of the selected checkbox.

I assume it's something like this (although I'm sure the syntax is incorrect)...

event.value = 0;

if (this.getField("CheckBox1#0").value!="Off") event.value=120;

else if (this.getField("CheckBox1#1").value!="Off") event.value=450;

else if (this.getField("CheckBox1#2").value!="Off") event.value=200;

else if (this.getField("CheckBox1#3").value!="Off") event.value=300;

else if (this.getField("CheckBox1#4").value!="Off") event.value=450;

Any help would be appreciated.

try67
Community Expert
July 7, 2019

What are the actual export values of these check-box fields?

sw777
Known Participant
July 7, 2019

Assessment, Capstone, Low, Medium, High

try67
Community Expert
September 23, 2017

Yes, that's easily done. For example, let's say you have a check-box called "Checkbox1" and you want to populate a field called "Text1" with 500 if it's checked, and zero otherwise.

Use this code as the custom calculation script of Text1:

event.value = (this.getField("Checkbox1").value=="Off") ? 0 : 500;

Participating Frequently
June 13, 2024

Hi! I was hoping you could help me with a code. I want to have a summary area that lists the checkboxes that were ticked.

 

Department name

Checkbox - Will you need graphics?

Checkbox - Minnie Mouse

Checkbox - Daffy Duck

 

If they check the question and the names, I want it to appear in a summary.

 

Could you please give me the steps and code to do this?

Participating Frequently
June 14, 2024

Thanks! when I'm typing the code in notepad and the link gets to be too long, how can I continue on the next line? A simple return doesn't seem to work.


I think it's working using Retirn now. This thing is so sensitive. I'll make a change and it won't update and then I'll have to delete it and re enter it a few time for it to work. Like it saves a cache or something.