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

JavaScript not saving

Community Beginner ,
Mar 27, 2025 Mar 27, 2025

So, I keep putting this script into a checkbox:

var checkbox = this.getField("Check Enclosed");
var fields = ["Signature", "Billing Address", "Name on Card", "CVV", "Exp Date", "Name", "Card"];
function setRequiredFields() {
    for (var i = 0; i < fields.length; i++) {
        var field = this.getField(fields[i]);
        field.required = checkbox.value === "Off";
    }
}
checkbox.setAction("MouseUp", "setRequiredFields();");
setRequiredFields();

 

And it works fine, but when I save and open the document again it replaces the script with just 

 

setRequiredFields();

Anyone know why this might be happening?

 

TOPICS
JavaScript , PDF , PDF forms
77
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 ,
Mar 27, 2025 Mar 27, 2025

Because that's what you're telling it to do in the penultimate line of your code... The first part of the code (until the end of the function) seems like it should be a doc-level script, although defining the check-box variable outside the function doesn't make much sense.

The line before last should be executed only once, from the JS Console.

The last line should be removed entirely.

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 ,
Mar 27, 2025 Mar 27, 2025

Because that's what you're telling it to do in the penultimate line of your code... The first part of the code (until the end of the function) seems like it should be a doc-level script, although defining the check-box variable outside the function doesn't make much sense.

The line before last should be executed only once, from the JS Console.

The last line should be removed entirely.

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 ,
Mar 27, 2025 Mar 27, 2025

The setAction field method writes the action script setRequiredFields();  You shouldn't be using this method as part of a field script.  It is more of an automation tool to write scripts or formats into fields.

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 ,
Mar 27, 2025 Mar 27, 2025
LATEST

The reason is this line:

checkbox.setAction("MouseUp", "setRequiredFields();");
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