Skip to main content
Participating Frequently
March 27, 2025
Besvarat

JavaScript not saving

  • March 27, 2025
  • 3 svar
  • 550 visningar

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?

 

Bästa svar av try67

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.

3 svar

Bernd Alheit
Community Expert
Community Expert
March 27, 2025

The reason is this line:

checkbox.setAction("MouseUp", "setRequiredFields();");
PDF Automation Station
Community Expert
Community Expert
March 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.

try67
Community Expert
try67Community ExpertSvar
Community Expert
March 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.