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

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

New Here ,
Feb 19, 2025 Feb 19, 2025

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}
}
157
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 ,
Feb 20, 2025 Feb 20, 2025

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

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 ,
Feb 20, 2025 Feb 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.

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

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 ,
Feb 21, 2025 Feb 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;

@+

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 ,
Feb 21, 2025 Feb 21, 2025
LATEST

[MOVED TO THE ACROBAT DISCUSSIONS]


Acrobate du PDF, InDesigner et Photoshopographe
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