Skip to main content
Inspiring
March 25, 2024
Answered

2 Caclculating Checkbox's

  • March 25, 2024
  • 1 reply
  • 1450 views

Scenaro: I am trying to create  2 checkbox's, where the result of the checked box is entered in the text box next to them.  Checkbox1 if checked, the value of $500 is added to the textbox with the 0, if Checkbox2 is selectected, $1000 will be added to the textbox with the 0.  Depending on which box is checked the total sum (including line items not shown) is added at the bottom of the document. 

Problem:  The problem I am having is that the total field at the bottom of the document adds this to the total if the either are the box are selected THEN deselected.  Event though they are set to "Off".   If the box's are not touched, they do not add to the total, but once either box is selecected, if it is deselected, it will still appear in the total.  How do I prevent this from happening?  

Here is the code I am using in the 0 text box:

event.value = 0;

if (this.getField("Corner1").value!="Off") event.value=500; if (this.getField("Corner2").value!="Off") event.value=1000;

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

 

 

This topic has been closed for replies.
Correct answer jacquelinie36313572rcye

This is a more complex issue that has multiple solutions. 

 

There are two ways to make the checkboxes mutually exclusive.

1) give them all the same name and different export values. This is the easy method

2) write a script that actively maintains the mutual exclusion.  This is more difficult. 

 

 

You also have two other problems, 

1) Mapping the check boxes to the related text field.

2) Managing the values in the text fields.

You've indicated that currently both of these items are managed with individual calculation scripts.  This works when the checkboxes have different names. But the scripts would need to be changed if the checkboxes were renamed to make them mutually exclusive, which is the better method.

 

If I was designing this from scratch, I would have either used group names for the checkboxes that had a matching component for the text field name, or made them all the same name and used the export value to indicate the related text field. I would also have written a single document level calculation function that would work for all of the related text fields.

 

But given what you already have it's probably easier to write a function for managing the mutual exclusion of the check boxes. 

 

Here's a document level function that is called from the MouseUp of every checkbox.

 

function MakeExclusive()
{
	if(event.target.value != "Off")
	{ // Only necessary when click field is checked, not unchecked
		var aChkFlds = ["CheckBoxA","CheckBoxB","CheckBoxC","CheckBoxD"];
		aChkFlds.map(a=>this.getField(a)).forEach(function(oFld){if((oFld != event.target) && (oFld.value != "Off")) oFld.value = "Off";});
	}
}

           

 

   


10 million Thanks, Worked like a Charm....AGAIN!!!! 8 Mutually exclusive text box's, for a novice its a thing to behold (I'm jumping up and down and the kids just don't get it)

And here it is for the PERFECTLY MUTUALLY EXCLUSIVE CHECKBOX.  After you add the Checkbox and it's corresponding Textbox, the below code is added to each Textbox lineitem associated with a Checkbox. 

Each Textbox associated with a checkbox has the code:

 

if (this.getField("CheckBoxA").value != "Off") {
// box is checked
event.value = 25110 ;
} else {
// box is unchecked
event.value = "" ;

 

THEN: Adding the MouseUp Java Scrip (for my specific situation):

{
if(event.target.value != "Off")
var aChkFlds = ["CheckBoxA","CheckBoxB","CheckBoxC","CheckBoxD","CheckBoxE","CheckBoxF", "CheckBoxG", "CheckBoxH"];
aChkFlds.map(a=>this.getField(a)).forEach(function(oFld){if((oFld != event.target) && (oFld.value != "Off")) oFld.value = "Off";});
}

1 reply

Thom Parker
Community Expert
Community Expert
March 25, 2024

The script looks good. There might be a calculation order issue.

Make sure the field that recieves the data comes before the total field.

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Inspiring
March 25, 2024

First, let me thank you for this invaluable assistance.  As a novice, I have built this entire contract with posts from other people in your thread.  You are truly my hero becaue my boss thinks I'm brilliant!

Yes I believe I have the order correct (see below).  The odd thing is I coped this script (and tweeked it a bit) from another post, the following coding isn't even on my document, but when I remove it the code stops working: this.getField("fullpageadregular").value!="Off.

"Fullpageadregular is not on my document it just happend to be in the code I found an copied, but strangly enough when I remove the line of script, my code no longer delineates between the two box, and only one amount is displayed, regardless which box is selected.

 

 

Inspiring
March 25, 2024

REORDERING WORKED!  I REORDERED EVERYTING EXACTLY AS IT APPEARS IN EACH LINE ITEM ON THE DOCUMENT, AND IT'S PERFECT!  OMG THANK YOU.  I had this problem with another line item, and it corrected that one as well.  THANK YOU MY HERO!