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

custom javascript if checkbox checked

Community Beginner ,
Oct 15, 2020 Oct 15, 2020

My knowledge of Javascript is very basic and I have a pdf form I cannot figure out the javascript.

I need to calculate taxes based on which checkbox is checked.

 

If checkbox 1 is checked, then need to calculate Federal tax (5%) on the "total", if checkbox 2 is checked then the value is 0, if checkbox3 is checked then a manual entry is to be made

 

 

TOPICS
PDF forms
4.0K
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 ,
Oct 15, 2020 Oct 15, 2020

For this you definately need a script.

Are these "checkboxes" mutually exclusive? i.e radio buttons.  Or are they completely different checkboxes?

 

So here's a script that assumes the "checkboxes" are really radio buttons named "TaxType", with export values of  "Choice1", "Choice2","Choice3". 

This script goes into the custom calculation on Tax field.

 

 

var nTotal = Number(this.getField("Total").value);
var nChoice = this.getField("TaxType").value;
// block manual entry
event.target.readonly = true;

switch(nChoice)
{
    case "Choice1":
      event.value = .05 * nTotal;
      break;
    case "Choice2":
      event.value = 0;
      break;
    case "Choice3":
      event.rc = false; // This allows for manual entry
      event.target.readonly = false;
      break;
    default:
      event.value = "";
      break;
}

 

 

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 ,
Oct 15, 2020 Oct 15, 2020

For this you definately need a script.

Are these "checkboxes" mutually exclusive? i.e radio buttons.  Or are they completely different checkboxes?

 

So here's a script that assumes the "checkboxes" are really radio buttons named "TaxType", with export values of  "Choice1", "Choice2","Choice3". 

This script goes into the custom calculation on Tax field.

 

 

var nTotal = Number(this.getField("Total").value);
var nChoice = this.getField("TaxType").value;
// block manual entry
event.target.readonly = true;

switch(nChoice)
{
    case "Choice1":
      event.value = .05 * nTotal;
      break;
    case "Choice2":
      event.value = 0;
      break;
    case "Choice3":
      event.rc = false; // This allows for manual entry
      event.target.readonly = false;
      break;
    default:
      event.value = "";
      break;
}

 

 

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 ,
Oct 15, 2020 Oct 15, 2020

it works perfectly.  Thank you so much

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 ,
Oct 20, 2020 Oct 20, 2020

On the same document, I am trying to calculate a percentage (GP%).  My script is very simple:  Margin/Total. However, the form user will get an error message "The value entered does not match the format of the field (GP).  The user can bypass by clicking OK but this is not ideal.  I understand that the error message happens when the value is 0 but I do not know how to fix that 

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 ,
Oct 20, 2020 Oct 20, 2020
LATEST

A common issue. Here's an article on the topic:

https://www.pdfscripting.com/public/Value-Entered-Does-Not-Match-Format.cfm?sd=40

 

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