Skip to main content
Known Participant
July 27, 2018
Answered

Custom Calculation Script vs JavaScript Action

  • July 27, 2018
  • 2 replies
  • 1583 views

The purpose of the form i am making is to have blank fields as NOT readonly and fields that are filled in as readonly. If the filled in fields need to be modifed, i have an edit button that turns all fields that are readonly as false and once ONE field is edited it disables editing unless you click the edit button again.

The PDF is working fine if i go to each field and paste the script below into is custom calculation script

{

var f = this.getField(event.target.name);

if (f.value == "")

f.readonly = false;

else

f.readonly = true;

}

However since i have many fields and files where i need to apply this to, I need a way that allows me to apply this script to all fields in the PDF.

What i tried doing was selecting all my fields BUT when I opened the properties tab it does not give the calculate tab to enter a custom calculation script (see below):

It does give the option to add a javascript action so I added the javascript with a mouse up trigger. I used the same code

{

var f = this.getField(event.target.name);

if (f.value == "")

f.readonly = false;

else

f.readonly = true;

}

So now each of these fields that I selected have a mouse up javascript action with the above code.

However the PDF does not behave the same. Now if i click the edit button it lets me edit any fields as I would like but once i go to another field editing is still enabled. I would like to disable editing once one field has been edited.

Also if click the edit button it lets me edit the field and if i click out and click back into the field I just edited, it will disable editing for that field but I am still able to edit other fields that I have not edited before.

In short is there a way i can modify my script so it will work the same when i had used it in a custom calculation script.

This topic has been closed for replies.
Correct answer George_Johnson

For what you want to do, I'd leave it as a custom calculate script, but it can be simplified to:

event.target.readonly = event.target.valueAsString;

Use some JavaScript in the interactive console (Ctrl+J) to apply it as the calculate script to all of the text fields in the form, something like:

// Initialize variables, set string representing custom calculate script

var i = 0, f, sJS = "event.target.readonly = event.target.valueAsString;";

// Apply the calculate script to all text fields

for (i; i < numFields; i += 1) {

    f = getField(getNthFieldName(i));

    if (f.type === "text") {

        f.setAction("Calculate", sJS);

    }

}

Paste that entire script in the console, select it all, and press Ctrl+Enter, or Enter on the numeric keypad. You'll then find that all of the text field have the calculate script applied.

2 replies

George_JohnsonCorrect answer
Inspiring
July 27, 2018

For what you want to do, I'd leave it as a custom calculate script, but it can be simplified to:

event.target.readonly = event.target.valueAsString;

Use some JavaScript in the interactive console (Ctrl+J) to apply it as the calculate script to all of the text fields in the form, something like:

// Initialize variables, set string representing custom calculate script

var i = 0, f, sJS = "event.target.readonly = event.target.valueAsString;";

// Apply the calculate script to all text fields

for (i; i < numFields; i += 1) {

    f = getField(getNthFieldName(i));

    if (f.type === "text") {

        f.setAction("Calculate", sJS);

    }

}

Paste that entire script in the console, select it all, and press Ctrl+Enter, or Enter on the numeric keypad. You'll then find that all of the text field have the calculate script applied.

Thom Parker
Community Expert
Community Expert
July 27, 2018

try this for adding the calculate script to all text fields.

var strScript = "var f = this.getField(event.target.name);if (f.value == '') f.readonly = false; else f.readonly = true;";

for(var i=0;i<this.numFields;i++)

{

   var oFld = this.getField(this.getNthFieldName(i));

   if(oFld.type == "text") 

       oFld.setAction("Calculate",strScript);

}

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
July 27, 2018

This worked as well!!

Thanks!