• Calculation resulting from checkbox selection in p...

# Calculation resulting from checkbox selection in pdf

Community Beginner ,
Sep 23, 2017 Sep 23, 2017

Copied

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.

TOPICS
PDF forms

Views

8.1K

Likes

Report

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

Adobe Community Professional , Sep 23, 2017 Sep 23, 2017

Then use this:

event.value = 0;

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

Likes

25 Replies 25
Sep 23, 2017 Sep 23, 2017

Copied

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;

Likes

Report

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

Copied

WOW! Thank you @try67ā may I ask how to complete it if there are two choices? So if one box is checked, it's 500, and if the other box is checked it's 700?

Likes

Report

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

Copied

I tried using this:

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

event.value = (this.getField("fullpageadregular").value=="Off") ? 0 : 700;

Likes

Report

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

Copied

No, that won't work... What if both boxes are ticked?

Likes

Report

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

Copied

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

Likes

Report

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

Copied

Then use this:

event.value = 0;

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

Likes

Report

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

Copied

YES!!!! You're a genius and a scholar and my hero!!!!!!! Thank you try67ā !!!!!

May I ask for another scenario? They also have an input field where they can enter certain booth numbers that are premium prices, so if they enter 301, the price is \$1,000, if they enter 302 the price is \$1,200 -- how would you do that or can that be done? In this case, the user enters a specific number

Likes

Report

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

Copied

You can use something like this:

if (this.getField("boothnumber").valueAsString=="301") event.value=1000;

if (this.getField("boothnumber").valueAsString=="302") event.value=1200;

etc.

Likes

Report

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

Copied

You're my BEST FRIEND! Thank you try67ā!!! That worked, BUT the user can actually enter more than one booth number (maximum 3) in that one field (separated by a comma) -- is it possible to add the amounts?

Likes

Report

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

Copied

Yes, but this is getting more complicated...

Likes

Report

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

Copied

If you wish I can create this script for you, for a small fee. You can send me the full specs to try6767 at gmail.com and we could discuss the details.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Feb 16, 2022 Feb 16, 2022

Copied

I have several boxes and they can check more than 1, how do I sum up the value of their choices? Thanks.

Likes

Report

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

Copied

There are many different ways to add-up checkbox selections. The exact solution depends on the exact details.

Also, you might start by learning a bit more about scripting PDF checkboxes:

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Feb 17, 2022 Feb 17, 2022

Copied

Thanks, I created radio checkboxes (for options where they can only choose 1) and regular checkboxes and assigned different values for each checkbox. I was then able to sum up the values based on their choices.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jul 18, 2022 Jul 18, 2022

Copied

LATEST

Thanks for this, I just have one question, what do i put in when it says "Line Number"? I tried Textbox29 which is where the total will go, nothing happens

Also I need to total a number of text boxes, is this possible?

Likes

Report

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

Copied

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.

Likes

Report

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

Copied

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

Likes

Report

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

Copied

Assessment, Capstone, Low, Medium, High

Likes

Report

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

Copied

Then you should use those values in your code, and drop the "#1", "#2", etc. notation. Something like this:

`event.value = 0;if (this.getField("CheckBox1").valueAsString=="Assessment") event.value=120;else if (this.getField("CheckBox1").valueAsString=="Capstone") event.value=450;// etc.else event.value = "";`

Likes

Report

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

Copied

Like magic! Thanks very much.

I had tried that previously but I didn't use the "==". Just one "=".

Likes

Report

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

Copied

That's a common mistake. That assigns one value to another, not compare them.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Sep 21, 2019 Sep 21, 2019

Copied

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.

Likes

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
New Here ,
Jun 07, 2020 Jun 07, 2020

Copied

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;

Likes

Report

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

Copied

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 PDFScripting
Use the Acrobat JavaScript Reference early and often

Likes

Report

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