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

Put zero in text field if checkbox is checked

Community Beginner ,
Aug 12, 2025 Aug 12, 2025

I have checkboxes K1 add-up checkbox, K2 add-up checkbox...K20 add-up checkbox  I also have text fields formated for numbers which are K1, K2...K20.  Finally, I have text fields formated for numbers which are K1_, K2_...K20_.

 

Numbers will be entered into K1, K2, etc.  Those numbers will copy into K1_, K2, etc.  So far, so good.  If I check the K1 add-up checkbox, I would like the number copied into K1_ to change to zero.  I am using the following custom calculation script in K1_:

 

if (this.getField("K1 add-up checkbox").isBoxChecked(0)) {
event.value = 0;
}

 

So far, this also works.  The problem is that if I check more than one checkbox, say K1 add-up checkbox and K2 add-up checkbox, the zero goes away in K2 add-up checkbox and it reverts to the number that is being copied from K2.

 

Hopefully, I have explained this sufficiently for someone to help me with what is going.  I'm thinking it mus have something to do with the custom calculation script.  Thanks for any help.

TOPICS
JavaScript , PDF , PDF forms
625
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
1 ACCEPTED SOLUTION
Community Expert ,
Aug 12, 2025 Aug 12, 2025

The only code that sets the "K1_" field should be in the calculation script for the "K1_" field. And that code should never use   this.getField("K1_") . Field values in a calculation are set through "event.value". 

 

Please use this script, and only this script in the "K1_" field.

 

if (this.getField("K1 add-up checkbox").isBoxChecked(0))
   event.value = 0;
else
   event.value = this.getField("K1").value;

 

You can read about calculations here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

 

 

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

View solution in original post

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 ,
Aug 12, 2025 Aug 12, 2025

How are you copying the number from K2 into K1_?

By the way, these are pretty confusing field names... You should consider something a bit more descriptive, but that's a different issue.

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 Beginner ,
Aug 12, 2025 Aug 12, 2025

The number is copied using this custom calculation script:  getField("K1_").value = getField("K1").valueAsString;

 

This is part of a 200+ page document, so the field names do have significance in the overall document (reference to the name of the schedule and the item in the schedule).

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 ,
Aug 12, 2025 Aug 12, 2025

If that calculation is on the "K1_" field, then it needs to use "event.value" to set the field value. 

If it is not on the "K1_" field, then it needs to be removed. 

Use the calculation for the "K1_" field provided above.

 

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 ,
Aug 13, 2025 Aug 13, 2025

You have conflicting calculations, then. You need to consolidate everything to a single script where you enter all of the logic of what value this field should have, and under what circumstances.

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 ,
Aug 12, 2025 Aug 12, 2025

In general, it sounds like your scripts are walking on top of each other.

If the calculation script is used, then only the calculation script should set the field value. If code somewhere else is setting the same field value, the scripts will have a conflict.

 

Something like this: calculation for "K1_". and this is the only script that sets the "K1_" value.  

if (this.getField("K1 add-up checkbox").isBoxChecked(0)) {
event.value = 0;
}
else
  event.value = this.getField("K1").value;

 

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 Beginner ,
Aug 12, 2025 Aug 12, 2025

That doesn't seem to help.  I'm thinking that there may be conflicting code as the full code that is in the K1_  field is:

 

getField("K1_").value = getField("K1").valueAsString;

 

if (this.getField("K1_ add-up checkbox").isBoxChecked(0)) {
event.value = 0;
}

 

If this is causing the problem, can the

if (this.getField("K1_ add-up checkbox").isBoxChecked(0)) {
event.value = 0;
}

 

be put somewhere in the "K1 add-up checkbox" as script so that "K1_" turns to zero when it is checked?

 

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 Beginner ,
Aug 12, 2025 Aug 12, 2025

As I think about it, putting the code into the checkbox probably won't help as the "getField("K1_").value = getField("K1").valueAsString;" still needs to be in the "K1_" field.  Thus, the conflict will persist.

 

Here's what I'm trying to accomplish:  If I enter a number in K1, want it to copy into K1_.  However, if I tick the checkbox, K1 should still contain the number and K1_ should change to zero.   The fields can't both be named K1 since if the checkbox is ticked, K1 should contain the number and K1_ should contain a zero.  Maybe there's a simpler way to do this.

 

 

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 ,
Aug 12, 2025 Aug 12, 2025

The only code that sets the "K1_" field should be in the calculation script for the "K1_" field. And that code should never use   this.getField("K1_") . Field values in a calculation are set through "event.value". 

 

Please use this script, and only this script in the "K1_" field.

 

if (this.getField("K1 add-up checkbox").isBoxChecked(0))
   event.value = 0;
else
   event.value = this.getField("K1").value;

 

You can read about calculations here:

https://www.pdfscripting.com/public/Calculating-field-values-and-more.cfm

 

 

 

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 Beginner ,
Aug 13, 2025 Aug 13, 2025
LATEST

That works.  Thank you!

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