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

Reset all form fields to default value

Participant ,
Oct 23, 2021 Oct 23, 2021

Copy link to clipboard

Copied

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 🙂

TOPICS
Create PDFs , How to , JavaScript , PDF forms

Views

2.7K

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
1 ACCEPTED SOLUTION
Community Expert ,
Oct 23, 2021 Oct 23, 2021

Copy link to clipboard

Copied

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

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

View solution in original post

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
Participant ,
Oct 23, 2021 Oct 23, 2021

Copy link to clipboard

Copied

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();
}

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 ,
Oct 23, 2021 Oct 23, 2021

Copy link to clipboard

Copied

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

 

 

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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
Participant ,
Oct 23, 2021 Oct 23, 2021

Copy link to clipboard

Copied

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.

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 ,
Oct 23, 2021 Oct 23, 2021

Copy link to clipboard

Copied

LATEST

I wrote the code as "doc.resetForm()" because the resetForm function is a member of the document object. The "this" is a Core JavaSript keyword that references the current object. It just so happens that it is the current document when used in a top level script in a document event, which is why all the sample code is written that way. But do not make the mistake of thinking that "this" is always the current document. It is not.

 

     

Thom Parker - Software Developer at PDFScripting
Use the Acrobat JavaScript Reference early and often

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