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

Javascript help: calculating

New Here ,
May 06, 2024 May 06, 2024

Hello there,

 

I've made a PDF (see attachment). This PDF, which you can test here (just a basic page) has multiple fields. All goes well if you start in the first field and work your way down to the secondlast field. In the last field (Premie1) the Net premium is showing. To show this, I'm working with a Javascript code on the field. The code first does three calculations, and then combines them showing the final Net premium. This works. 

 

But  when I change the input of a field, for example the first field (Veld1Premie1), the total showing in the last field gets corrupt.

 

For example:

I choose 'apotheker' in the first field. Then I choose the amount of PTE of 1. It shows a Net premium of 250. All good.

When I switch the profession from apotheker to 'cardioloog zonder invasieve procedures' for example then the amount of Net premium changes to something which I can't explain. 

 

Summary of my problem: if the form is clean and you fill in every field step by step from top to bottom it works. Whenever you change a field afterwards, the total Net premium isn't correct. 

 

This is the JS code (the field calculation order of the PDF is exactly in the order as the form is)

 

// Haal de waarden op van de velden SubPremie1, FTE1, PTE1 en Klasse1
var subPremie1Value = this.getField("SubPremie1").value;
var fte1Value = this.getField("FTE1").value;
var pte1Value = this.getField("PTE1").value;
var klasse1Value = this.getField("Klasse1").value;
var sta1Value = this.getField("STA1").value;


// First calculation
var firstCalculation = subPremie1Value * fte1Value;

// Second calculation
var secondCalculation;
if (klasse1Value === "Klasse 4" || klasse1Value === "Klasse 5a" || klasse1Value === "Klasse 5b") {
    secondCalculation = (subPremie1Value * pte1Value) * 0.6;
} else {
    secondCalculation = subPremie1Value * pte1Value;
}

var thirdCalculation = (subPremie1Value * 0.5) * sta1Value;


// Calculation of total
getField("Premie1").value = firstCalculation + secondCalculation + thirdCalculation;

 

 

TOPICS
JavaScript , PDF , PDF forms
380
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
New Here ,
May 06, 2024 May 06, 2024

It already would help if I could make a button next to every Net Premium output field. When someone clicks the button, the calculation in the JS needs to be done again. Is this possible? See it like a refresh button.

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 ,
May 06, 2024 May 06, 2024
LATEST

Change this line:

getField("Premie1").value = firstCalculation + secondCalculation + thirdCalculation;

To:

event.value = firstCalculation + secondCalculation + thirdCalculation;

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