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

Help with JavaScript?

Community Beginner ,
Apr 16, 2024 Apr 16, 2024

Copy link to clipboard

Copied

I have an adobe acrobat form with four fields,

  • the first is called Type of Assignment, and it's a multiple choice field with these options to choose from: Select One, Partial, and, Full.
  • The second field is Number of Leases and is a text field.
  • The third field is also a text field, this one is called Cost per Lease.
    I need the third to field display $10 when the user selects Partial in the first field, and $25 when the user selects Full. Choosing Select One from the first field should do nothing other than reset Cost per Lease to an empty field, I suppose.
  • The fourth field on the form is called Amount Due.
    When the user enters a number into the Number of Leases field, the Amount Due field should display the calculation of Number of Leases X Cost per Lease.

So the user manipulates two fields which causes the other fields to automatically update.  I've managed to cobble together this simple script(s).

 

1) This script is added to field one, Type of Assignment:

 

this.getField("Cost per Lease").value = ""; // Reset Cost per Lease on any change

var typeSelection = this.getField("Type of Assignment").value;

if (typeSelection == "Partial") {
this.getField("Cost per Lease").value = "$10";
} else if (typeSelection == "Full") {
this.getField("Cost per Lease").value = "$25";
}

 

2.) This script is associated with field 2, Number of Leases

 

// Previous value holder
var prevNumLeases = "";

// Function to check for value change
function hasValueChanged(fieldName) {
var currentField = this.getField(fieldName);
var currentValue = currentField.value;

if (currentValue != prevNumLeases) {
prevNumLeases = currentValue; // Update previous value
return true;
}
return false;
}

// Script to run on both Mouse Up and On Blur triggers
if (hasValueChanged("Number of Leases")) {
// Call your existing Script 2 here to calculate Amount Due
}

 

The end result is, I choose one of the two options in field one and nothing happens.  I click in field 2 and enter the number of leases and click enter (or just click off or tab away from the field) and nothing happens.  If I then click back into field one, though, the correct results are displayed in fields 3 and 4, but only then.

 

Any suggestions?  Here's the pdf.

 

Thanks.

 

PS. when I posted this I received a message that invalid HTML was found in the message body and that it had been stripped.  I don't see any changes to the JavaScript, so hopefully it's all intact.

TOPICS
JavaScript , PDF forms

Views

173

Translate

Translate

Report

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

correct answers 1 Correct answer

Community Expert , Apr 16, 2024 Apr 16, 2024

Don't set dollar sign as string when you use that value in calculation, just format the field as number and add currency sign.
use this as 'Validate' script of "Type of Assignment" field:

var cost = this.getField("Cost per Lease");
if(event.value == "Partial")
cost.value = 10;
else if(event.value == "Full")
cost.value = 25;
else
cost.value = "";

 

For the calculation go to 'Calculate' tab of "Amount Due" field select 'Value is the' then select product(x) and 'Pick' "Number of Leases" and "Cost pe

...

Votes

Translate

Translate
Community Expert ,
Apr 16, 2024 Apr 16, 2024

Copy link to clipboard

Copied

Don't set dollar sign as string when you use that value in calculation, just format the field as number and add currency sign.
use this as 'Validate' script of "Type of Assignment" field:

var cost = this.getField("Cost per Lease");
if(event.value == "Partial")
cost.value = 10;
else if(event.value == "Full")
cost.value = 25;
else
cost.value = "";

 

For the calculation go to 'Calculate' tab of "Amount Due" field select 'Value is the' then select product(x) and 'Pick' "Number of Leases" and "Cost per Lease" fields.

 

Here is your file with changes made:

https://drive.google.com/file/d/1K4rMIhgr5UCq6qt0-C1Y4jqvCb2BO_ig/view?usp=sharing 

 

Also in "Type of Assignment" field go to 'Options' tab and check 'Commit selected value immediately'.

Votes

Translate

Translate

Report

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 ,
Apr 17, 2024 Apr 17, 2024

Copy link to clipboard

Copied

LATEST

Thank you, Ma'am.  I've not done this kind of work before, using scripts or even calculation in PDFs, and your solution was eye opening for me.  Appreciate it.

Votes

Translate

Translate

Report

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 ,
Apr 16, 2024 Apr 16, 2024

Copy link to clipboard

Copied

There are errors in your code. Check the JS Console (Ctrl+J) after changing the value of the drop-down field.

Votes

Translate

Translate

Report

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