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

make multiple fields able to be manually edited after receiving value from Java Script Calculation

Community Beginner ,
May 27, 2024 May 27, 2024

Copy link to clipboard

Copied

Hello, I'm new and still learning in creating PDF forms using Java Script Calculation. I'm in the process to create a form with fields that have multiple Java Script codes to give an output based on individual/multiple input fields for examples the codes are as follow:

Java Script Code for output field 1:

 

var field1Value = this.getField("text10").value;

var field2Value = this.getField("Dropdown1").value;

if(field1Value == 0)
this.getField("Exchange for").value = " ";

else
this.getField("Exchange for").value = util.printf("%,0.2f",field1Value) + " " + field2Value;

 

Java Script for output field 2 which functions that output field 2 will shows words based on which checkboxes chosen on previous field:

 

var u = this.getField("Group3").value;

if(u != "2")

{this.getField("At").value = "Sight";
} else

if(u != "1")

{this.getField("At").value = "Usance";
} else

{this.getField("At").value == Null;
}

 

Java Script for output field 3 which functions to convert number inputted on input field into words:

 

// Field-level script for 'text10'
{
var text10Value = event.target.value;
var dropdown1Value = this.getField("Dropdown1").value;
var resultField = this.getField("At the Sum of 1");

if (text10Value == 0 || text10Value === "") {
resultField.value = " ";
} else {
resultField.value = ConvertToWords(text10Value) + " " + dropdown1Value;
}
};

 

of all the codes I mentioned above, all works fine individually, however my form have additional requirement where all of the output fields (output field 1, output field 2, output field 3) have to be able to be manually edited without changing the values or revert to values calculated by Java Script code from values of the source/input field.  i have tried using this code on my output fields to enable manual edit on all of my output fields:

 

// Disable auto-update for 'At the Sum of 1'
this.getField("At the Sum of 1").setAction("Keystroke", "event.rc = true;");

// Function to set keystroke action for multiple fields
function setKeystrokeActionForFields(fieldNames) {
for (var i = 0; i < fieldNames.length; i++) {
var fieldName = fieldNames[i];
var field = this.getField(fieldName);
if (field) {
field.setAction("Keystroke", "event.rc = true;");
} else {
console.warn("Field '" + fieldName + "' not found.");
}
}
}

// Call the function and pass an array of field names
setKeystrokeActionForFields(["Exchange for", "At", "At the Sum of 1"]);

 

however the code above only work if i only manually edit 1 output field, if i try to edit the value of another output field the previous edited output field will revert back into values based on Java Script calculation of the input field, i needed the output field to be editable for print purposes while also so the output fields will give values if the input on the input fields changes. please help me regarding how to do that?

 

sorry for the long questions, i tried to describe it as detailed as possible to prevent ambiguity, and as i didnt really understand regarding Java Script, all the codes above are the result of surfing this forum.

 

thanks in advance!

TOPICS
Edit and convert PDFs , How to , JavaScript , PDF , PDF forms

Views

152

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 , May 27, 2024 May 27, 2024

You can use slightly modified script in each field as 'On Blur' action or you can use 'event.source' to allow manual edit of the field, like this:

 

if (event.source && (event.source.name=="text10" || event.source.name=="Dropdown1")) {
var field1Value = this.getField("text10").value;

var field2Value = this.getField("Dropdown1").value;

if(field1Value == 0)
this.getField("Exchange for").value = " ";

else
this.getField("Exchange for").value = util.printf("%,0.2f",field1Value) + " " + field2Value;
}

Man

...

Votes

Translate

Translate
Community Expert ,
May 27, 2024 May 27, 2024

Copy link to clipboard

Copied

You can use slightly modified script in each field as 'On Blur' action or you can use 'event.source' to allow manual edit of the field, like this:

 

if (event.source && (event.source.name=="text10" || event.source.name=="Dropdown1")) {
var field1Value = this.getField("text10").value;

var field2Value = this.getField("Dropdown1").value;

if(field1Value == 0)
this.getField("Exchange for").value = " ";

else
this.getField("Exchange for").value = util.printf("%,0.2f",field1Value) + " " + field2Value;
}

Manually entered text will not be overwritten as long as source fields doesn't change their value.

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 ,
May 27, 2024 May 27, 2024

Copy link to clipboard

Copied

Dear Miss Nesa,

 

sorry for the late reply, i've incorporated your code on my field level scripts particularly the one you mentioned above:

if (event.source && (event.source.name=="text10" || event.source.name=="Dropdown1")) {}

 

it works! however i have a follow up question, i have Button Function in my form where i set the action to clear fields value of input form if its clicked which i name "clear1". is it possible to incorporate this button as well in your code Miss Nessa? for example like this:

 

if (event.source && (event.source.name=="text10" || event.source.name=="Dropdown1" || event.source.name=="clear1")) {}

 

or it required new set of code?

 

thanks in advance for the info!

 

Kind Regards

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 ,
May 27, 2024 May 27, 2024

Copy link to clipboard

Copied

No.

There is no point of adding a button that clears all fields to that script.

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 ,
May 27, 2024 May 27, 2024

Copy link to clipboard

Copied

LATEST

Dear Miss Nessa,

 

okay, thank you for the info, i just thought of another solution of using the "clear1" button to not only reset values from the input fields, but also reset value from output fields. 

 

I want to thank you as well for your previous answers to another posts as it helped me to use Java Script on my PDF Form.

 

once again thank you Miss Nessa! i will mark your previous answer as the correct answer.

 

Kind Regards 

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