Skip to main content
Participant
February 20, 2025
Question

Multiplying a Text Field if a Checkbox is Checked, while freely being able to Uncheck it.

  • February 20, 2025
  • 3 replies
  • 433 views

I want to have a text field, "MA", with an initial value typed into it ("10"). When a seperate checkbox is checked ("CRITICAL1"), I want the value in MA to be halved ("5"). The script I wrote does that, but I'd also like those changes to revert when the box is unchecked. As it works now, unchecking the box does not change MA back to 10 from 5, and a subsequent recheck would halve MA again to sqrt(5).

The script below is a fix I tried to implement, where I multiply by 2 when the box is unchecked to cancel out the halving, but I've ran into a different problem where the initial number I input into MA is doubled from the start (e.g. when filling out MA for the first time, I write "10" and it becomes "20"). It's important to the document that MA works with any input, not just the one's I've used in my example. Any help?

var v1 = this.getField("CRITICAL1").isBoxChecked(0);

function test(){
if(v1){event.value = event.target.value*0.5;}
else{event.value = event.target.value*2}
}

3 replies

JR Boulay
Community Expert
Community Expert
February 21, 2025

[MOVED TO THE ACROBAT DISCUSSIONS]

Acrobate du PDF, InDesigner et Photoshopographe
bebarth
Community Expert
Community Expert
February 21, 2025

Hi,
You could use a global variable and an On Blur Action Script...

// Document Script
if (this.getField("CRITICAL1").isBoxChecked(0)) global.realValue=this.getField("MA").value*2;
else global.realValue=this.getField("MA").value;

// On Blur "MA" Field Script
global.realValue=event.value;
if (this.getField("CRITICAL1").isBoxChecked(0)) this.getField("MA").value=global.realValue*0.5;


// Mouse Up "CRITICAL1" Check Box Field Script
if (event.target.value!="Off") this.getField("MA").value=global.realValue*0.5;
else this.getField("MA").value=global.realValue;

@+

try67
Community Expert
Community Expert
February 20, 2025

Why did you put the code in a function, and then not call it?

try67
Community Expert
Community Expert
February 20, 2025

The real problem is that your code will run over and over again, each time the value of any field is changed, and will therefore continue to make the value smaller (or larger). You need to either make the code dependent on the triggering field being the check-box in question, or move it to the check-box itself and have it change the value of the text field, instead of putting it under the text field's own calculation script.

Thom Parker
Community Expert
Community Expert
February 21, 2025

To add to Try67's analysis. If a field value needs to be changed in a certain way, then the original value has to be preserved. Otherwise it can never go back. Calculations need to be a linear progression. It starts somewhere stable and then goes somewhere else. You don't have a stable starting place.   Think about that. 

 

Using the MouseUp event will provide an approximation. But it is not restoring the original value. Each time the number is divided and multiplied it losses data. Depending on the initial value, it could either maintain a stable value, or become unstable. So this is not a good strategy. 

 

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