Skip to main content
Known Participant
October 23, 2021
Answered

Reset all form fields to default value

  • October 23, 2021
  • 1 reply
  • 3647 views

Hello,

it is me again. 🙂

I am trying to reset a form when clicking a reset-button.

Currently i used the function resetForm(Fields) but i want to reset the fields to the default value which is given in the preferences/settings.

 

So i thought about looping all fields and put value to defaultValue but then my Acrobat freezes.

Previous Script:

var fieldstoteset = ["Field1", "Field2", "Field3"];
if(4==app.alert("The form will be resetted.\n\nAll data will be removed! Continue?",1,2)) {
    this.resetForm(fieldstoteset );
    this.getField("top").setFocus();
}

New Try:

if(4==app.alert("The form will be resetted.\n\nAll data will be removed! Continue?",1,2)) {
    for (var i = 0; i < this.numFields; i++) {
        var fieldname = getField(getNthFieldName(i);
        this.getField(fieldname).name).value = this.getField(fieldname).defaultValue;
    }
    this.getField("top").setFocus();
}

 

Many thanks in advance for your hints 🙂

This topic has been closed for replies.
Correct answer Thom Parker

The "doc.resetForm()" function already does this.  There is no need for an explicit script. 

 

 

1 reply

FireholdAuthor
Known Participant
October 23, 2021

EDIT: Sorry, have a typing mistake in the new script:

if(4==app.alert("The form will be resetted.\n\nAll data will be removed! Continue?",1,2)) {
    for (var i = 0; i < this.numFields; i++) {
        var fieldname = getField(getNthFieldName(i));
        this.getField(fieldname).value = this.getField(fieldname).defaultValue;
    }
    this.getField("top").setFocus();
}
Thom Parker
Community Expert
Thom ParkerCommunity ExpertCorrect answer
Community Expert
October 23, 2021

The "doc.resetForm()" function already does this.  There is no need for an explicit script. 

 

 

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
FireholdAuthor
Known Participant
October 23, 2021

Thank you very much, didn't knew i can fire the function without the field-names.

For the code i just needed to replace "doc" with "this" because otherwise it comes an error that doc is not defined.