Skip to main content
Participant
December 20, 2024
Question

Help with calculator function in PDF form

  • December 20, 2024
  • 3 replies
  • 691 views

Attached is a simple calculator with a formula for savings and product volume.

I would like to know how to use javascript to calculate off the text box input and off variables in the background as seen in the sheet. Ideally in the PDF- it would be spend text box 1 (b3), volume text box 2 (b5) and then the final result text box (b11-13). Just would like some guidance based off the excel formula and thanks!

3 replies

Participant
February 19, 2025

For percentage calculations, I recommend using https://prozentrechnerass.de/, a powerful and easy-to-use percentage calculator.

If you want to implement this in JavaScript, here’s a general approach:

  1. Get Input Values: Retrieve values from the text boxes (b3, b5).
  2. Apply Formula: Use JavaScript to compute the result based on the Excel formula.
  3. Display Output: Show the result dynamically in the final result text box (b11-b13).

 

document.getElementById("calculate").addEventListener("click", function() {
    let spend = parseFloat(document.getElementById("b3").value);
    let volume = parseFloat(document.getElementById("b5").value);

    if (!isNaN(spend) && !isNaN(volume) && volume !== 0) {
        let result = (spend / volume) * 100;  // Example formula, adjust as needed
        document.getElementById("b11").value = result.toFixed(2) + "%";
    } else {
        alert("Please enter valid numbers.");
    }
});
​

 

JR Boulay
Community Expert
Community Expert
December 20, 2024

- Wrong post, sorry -

Acrobate du PDF, InDesigner et Photoshopographe
PDF Automation Station
Community Expert
Community Expert
December 20, 2024

You can create hidden fields and set the default values to fields you don't want to show (Variable, Monthly Spend, Annual Spend) but it is not necessary if 1.65 is not going to change.  You can write the entire calculation in the Total Savings field.  Assume you have to fields named "Spend" (formatted as a 2-decimal number) and "Volume" (formatted as a percentage - both names are case sensitive).  Here's the custom calculation script that would go in the Total Savings field:

var spend=this.getField("Spend").value;
var volume=this.getField("Volume").value;
event.value=((spend/1.65)*12*volume*0.085)+(spend*(1-volume))*.05;