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

Reset a form - need help with javascript

Community Beginner ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

Hello,

I created an action button that clears a form. I used the JS below that provides the user a warning before they clear the form. However, if they select "no" it still clears it. How do I fix it?

 

Here's what I have so far:

 

if( app.alert({
cMsg: "Are you sure you want to clear this form?",
cTitle: "Reset Form Warning",
nIcon: 2,
nType: 2
}) == 4) {
this.resetForm();
}

TOPICS
Acrobat SDK and JavaScript

Views

10.1K

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

correct answers 2 Correct answers

Community Expert , Jan 06, 2020 Jan 06, 2020

There is nothing wrong with this code. If the form is still being cleared, then there is something else going on.

Are there more actions associated with the button? Or other code in the script? Maybe something leftover from your initial development?

Votes

Translate

Translate
Community Expert , Jan 06, 2020 Jan 06, 2020

Are you clearing the form and running the Javascript? If you have action to also clear form it will do both, which means clearing the form even if you press no here (and resetting it twice if you press yes). Remember you can have several events that trigger actions/scripts, so check that you don't have something happening on "Mouse Down", "Click" and/or "Mouse Up"

Votes

Translate

Translate
Community Expert ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

There is nothing wrong with this code. If the form is still being cleared, then there is something else going on.

Are there more actions associated with the button? Or other code in the script? Maybe something leftover from your initial development?

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
Explorer ,
Sep 10, 2021 Sep 10, 2021

Copy link to clipboard

Copied

Is there a way to script a condition that will have the reset form "Are you sure you want to clear this form?" warning pop up, but when clicking  "Yes" it resets the majority of the form except for 5 specific fields i don't want to be reset even when "Yes" is selected?

 

Some sort of "If" script statement maybe entered into my clear form button?

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 ,
Sep 10, 2021 Sep 10, 2021

Copy link to clipboard

Copied

The first part of your question was answered above. The second part is trickier. You can't specify which fields not to reset, only which ones to reset. So the solution would be to construct a list of all the field names in the field, except those five fields, and then supply that as a parameter to the resetForm command.

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
Explorer ,
Sep 10, 2021 Sep 10, 2021

Copy link to clipboard

Copied

Bummer 😞 

That was what I was trying to avoid just because I have like a 100 fields in the form lol.

Thanks for the heads up!  🙂

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 ,
Sep 10, 2021 Sep 10, 2021

Copy link to clipboard

Copied

Well, you don't have to actually write them all out. You can use a loop for that, like this:

 

var fieldsNotToReset = ["Text1", "Text10", "Text24", "Radio1", "Dropdown5"]; // Replace with actual field names
var fieldsToReset = [];
for (var i=0; i<this.numFields; i++) {
	var fname = this.getNthFieldName(i);
	var f = this.getField(fname);
	if (f==null) continue;
	if (f.type=="button") continue;
	if (fieldsNotToReset.indexOf(fname)!=-1) continue;
	fieldsToReset.push(fname);
}
this.resetForm(fieldsToReset);

 

 

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
Explorer ,
Sep 10, 2021 Sep 10, 2021

Copy link to clipboard

Copied

BAM!  🙂

 

It works!  And just what I needed!

 

Now when I click the clear form button it populates the warning message and then when clicking "Yes" it resets the entire form except for those 5 specific fields I don't want reset/cleared!

Thanks a ton once again @try67 !!!!  

 

For those who are javascript beginners like me, below is the javascript I used for my clear form button to execute the action above and seems to do the trick.  Credit to @PSURAM original posted script and @try67 's additional help.  Hope this is clear and helpful to other beginners searching here within community that can use this function.  (Note: be sure to replace the FIELDNAMES with your appropriate/actual field name(s):

 

if( app.alert({
cMsg: "Are you sure you want to clear this form?",
cTitle: "Reset Form Warning",
nIcon: 2,
nType: 2
}) == 4)

{

var fieldsNotToReset = ["FIELDNAME1", "FIELDNAME2", "FIELDNAME3", "FIELDNAME4", "FIELDNAME5"];

var fieldsToReset = []; for (var i=0; i<this.numFields; i++) { var fname = this.getNthFieldName(i); var f = this.getField(fname); if (f==null) continue; if (f.type=="button") continue; if (fieldsNotToReset.indexOf(fname)!=-1) continue; fieldsToReset.push(fname); } this.resetForm(fieldsToReset);
}

 

 

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
New Here ,
Sep 27, 2021 Sep 27, 2021

Copy link to clipboard

Copied

This code to reset works well. However, it once reset does not allow selecting a field again.

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 Beginner ,
Jun 28, 2023 Jun 28, 2023

Copy link to clipboard

Copied

Hey there! Can I use the same script to create a loop to reset fields after I use the "create a new page" feature. I have a product that adds a new page with the same fillable field with unique field names each time you click the action button so that you use the fields in the new page and they don't write over eachother. I need a javascript for a "clear fields" that ONLY clears the fields on THAT SPECIFIC PAGE not the entire document. Any idea on how I write that script?

 

*When I use the reset form drop down in "actions" it consistently deletes the fields on every new page created. 

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 ,
Jun 30, 2023 Jun 30, 2023

Copy link to clipboard

Copied

LATEST

It sounds like you want to reset fields on a page spawned from a template.

If this is true, then you can take advantage of the renaming pattern used by templates by performing the reset from a button on that page, because the button will be renamed in the same way.

 

You'll notice that the fields are renamed with the page number and template name like this

"P#.templateName"

 

All the reset script needs to do is extract this prefix and then use it directly in the resetForm function.

Like this:

var aNameParts = event.targetName.split(".");
var strPrefix = aNameParts[0] + "." + aNameParts[1];
this.resetForm(strPrefix);

 This script will only work on a spawned page, not the original template. The original template should be hidden.

 

 

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
Community Expert ,
Jan 06, 2020 Jan 06, 2020

Copy link to clipboard

Copied

Are you clearing the form and running the Javascript? If you have action to also clear form it will do both, which means clearing the form even if you press no here (and resetting it twice if you press yes). Remember you can have several events that trigger actions/scripts, so check that you don't have something happening on "Mouse Down", "Click" and/or "Mouse Up"

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 Beginner ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

Thanks that was it. I just needed to remove the duplicate "reset a form" action. Works perfectly now. 

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
New Here ,
Jan 07, 2020 Jan 07, 2020

Copy link to clipboard

Copied

Cancelar

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
New Here ,
Dec 28, 2020 Dec 28, 2020

Copy link to clipboard

Copied

Is there a way to target specific fields with this code? I dont want to clear everything.

I would like only to target dollar amounts in my form.

Thanks in advance

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 ,
Dec 29, 2020 Dec 29, 2020

Copy link to clipboard

Copied

Sure:

this.resetForm(["Field1", "Field2", "Field3"]);

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
Explorer ,
Aug 20, 2021 Aug 20, 2021

Copy link to clipboard

Copied

I'm having the same issue as original thread.

 

On my form I have a check box to add a driver and driver information to the form.  When the box is checked it makes specific fields visible to fill out, but then become hidden when box is unchecked.  The default state of the checkbox is "unchecked".

 

I want the user to be able to after having "checked" the box and filled out the requested data into to the visible fields to be able to have the option to "uncheck" the box and clear/reset the responses entered in any/all of the displayed fields if say they entered data in error or just changed their mind.  But once the user attempts to "uncheck" the box I want an alert pop up to with a reset warning alert to appear so they can confirm they indeed wish to remove driver.  The message reads: "Are you sure you want to remove and delete this driver info?"

 

My issue is I can't seem to get the "No" response to "Are you sure you want to remove and delete this driver info?" warning pop-up to cancel the reset action and return back to the form.  Both actions in the alert box "Yes" or "No" are reseting the form.  Can anyone help review my mouse up event script to see where the error is or how to fix that

 

if (event.target.value != "Yes"){
this.getField("Driver1 First Name").display = display.hidden;
this.getField("Driver1 Last Name").display = display.hidden;
this.getField("Driver1 DOB").display = display.hidden;
this.getField("Driver1 DL Number").display = display.hidden;
this.getField("Driver1 DL State").display = display.hidden;
this.getField("Driver1CDL").display = display.hidden;
this.getField("Driver1 DL State").display = display.hidden;
this.getField("Driver1Incidents").display = display.hidden;
this.getField("Driver1SR22").display = display.hidden;
if( app.alert({
cMsg: "Are you sure you want to remove and delete this driver info?",
cTitle: "Reset Form Warning",
nIcon: 1,
nType: 2
}) == 3);

this.resetForm(["Driver1 First Name", "Driver1 Last Name", "Driver1 DOB", "Driver1 DL Number", "Driver1 DL State", "Driver1CDL", "Driver1 DL State", "Driver1Incidents", "Driver1SR22"]);

}

else{
this.getField("Driver1 First Name").display = display.visible;
this.getField("Driver1 Last Name").display = display.visible;
this.getField("Driver1 DOB").display = display.visible;
this.getField("Driver1 DL Number").display = display.visible;
this.getField("Driver1 DL State").display = display.visible;
this.getField("Driver1CDL").display = display.visible;
this.getField("Driver1 DL State").display = display.visible;
this.getField("Driver1Incidents").display = display.visible;
this.getField("Driver1SR22").display = display.visible;}

 

I'm having the same issue as original thread.

 

On my form I have a check box to add a driver and driver information to the form.  When the box is checked it makes specific fields visible to fill out, but then become hidden when box is unchecked.  The default state of the checkbox is "unchecked".

 

I want the user to be able to after having "checked" the box and filled out the requested data into to the visible fields to be able to have the option to "uncheck" the box and clear/reset the responses entered in any/all of the displayed fields if say they entered data in error or just changed their mind.  But once the user attempts to "uncheck" the box I want an alert pop up to with a reset warning alert to appear so they can confirm they indeed wish to remove driver.  The message reads: "Are you sure you want to remove and delete this driver info?"

 

My issue is I can't seem to get the "No" response to "Are you sure you want to remove and delete this driver info?" warning pop-up to cancel the reset action and return back to the form.  Both actions in the alert box "Yes" or "No" are reseting the form.  Can anyone help review my mouse up event script to see where the error is or how to fix this issue.  Many Thanks!

 

if (event.target.value != "Yes"){
this.getField("Driver1 First Name").display = display.hidden;
this.getField("Driver1 Last Name").display = display.hidden;
this.getField("Driver1 DOB").display = display.hidden;
this.getField("Driver1 DL Number").display = display.hidden;
this.getField("Driver1 DL State").display = display.hidden;
this.getField("Driver1CDL").display = display.hidden;
this.getField("Driver1 DL State").display = display.hidden;
this.getField("Driver1Incidents").display = display.hidden;
this.getField("Driver1SR22").display = display.hidden;
if( app.alert({
cMsg: "Are you sure you want to remove and delete this driver info?",
cTitle: "Reset Form Warning",
nIcon: 1,
nType: 2
}) == 3);

this.resetForm(["Driver1 First Name", "Driver1 Last Name", "Driver1 DOB", "Driver1 DL Number", "Driver1 DL State", "Driver1CDL", "Driver1 DL State", "Driver1Incidents", "Driver1SR22"]);

}

else{
this.getField("Driver1 First Name").display = display.visible;
this.getField("Driver1 Last Name").display = display.visible;
this.getField("Driver1 DOB").display = display.visible;
this.getField("Driver1 DL Number").display = display.visible;
this.getField("Driver1 DL State").display = display.visible;
this.getField("Driver1CDL").display = display.visible;
this.getField("Driver1 DL State").display = display.visible;
this.getField("Driver1Incidents").display = display.visible;
this.getField("Driver1SR22").display = display.visible;}

 

Anthony5E5C_0-1629470539028.png

Anthony5E5C_1-1629470605664.png

 

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 ,
Aug 20, 2021 Aug 20, 2021

Copy link to clipboard

Copied

The return value from the alert box for a "Yes" answer is 4, not 3. 

 

 

 

 

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
Explorer ,
Aug 23, 2021 Aug 23, 2021

Copy link to clipboard

Copied

Hello Thom!

 

Thanks for taking a look.  I've learned a lot reading through several of your post which has helped me or shown what is possible in this being my first venture into dynamic form creation and specifically javascripting.

 

Per your feedback, I did update that return value from 3 to 4 in that part of the script you indicated, but I'm still having the issue of when the user unchecks the box to trigger the confirmation question pop-up, and clicks the "No" response, that the user does not want to remove and delete said info, it is still deleting and hidiing the specific fields the message is tied to. 

 

Is there something else I'm missing to add or change in the script?

 

Not sure if matters, but the script in this specific checkbox fields mouse up event is a bit busy.  Do i maybe need to break or separate out something in order for the pop-up events to work as I'd like?

 

Thanks!

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

Copy link to clipboard

Copied

Yes, remove the semicolon from the end of the "if" with the alert in it. That semicolon is telling the JS parser that there is no other code in the if block, so the next line is always executed, cause it's not in the "if" block.  Also, the code does not need to explicitly call out all the alert function arguments.  

 

if( app.alert("Are you sure you want to remove and delete this driver info?",1,2,"Reset Form Warning") == 3)
    this.resetForm(["Driver1 First Name", "Driver1 Last Name", "Driver1 DOB", "Driver1 DL Number", "Driver1 DL State", "Driver1CDL", "Driver1 DL State", "Driver1Incidents", "Driver1SR22"]);

 

This code could be further simplified if all the "Driver1" fields were named like this:

"Driver1.FirstName",  "Driver1.LastName", etc...

 

This groups all the fields together under the "Driver1" prefix, so now you can reset all the Driver 1 fields like this.

this.resetForm(["Driver1"]);

 

 

 

 

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
Explorer ,
Aug 23, 2021 Aug 23, 2021

Copy link to clipboard

Copied

It's worksing now!  🙂

 

Thanks for the help, explaination and code simplification suggestion as well!  

 

I'm pretty much at the finish line with this form, just putting on final touches I catch that can be made better when testing out, so I'll work on maybe later going in and renaming the fields to take advantage of the simplication tip.

 

Although I have another form I'm going to begin soon soon so having that info will be super helpful!

 

 

 

 

 

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