Skip to main content
Known Participant
January 6, 2020
Answered

Reset a form - need help with javascript

  • January 6, 2020
  • 4 replies
  • 15329 views

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

This topic has been closed for replies.
Correct answer Lukas Engqvist

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"

4 replies

Participating Frequently
December 29, 2020

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

try67
Community Expert
Community Expert
December 29, 2020

Sure:

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

Known Participant
August 20, 2021

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

 

 

PSURAMAuthor
Known Participant
January 7, 2020

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

Participant
January 7, 2020
Cancelar
Lukas Engqvist
Community Expert
Lukas EngqvistCommunity ExpertCorrect answer
Community Expert
January 7, 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"

Thom Parker
Community Expert
Community Expert
January 6, 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?

Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
September 10, 2021

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?

try67
Community Expert
Community Expert
September 10, 2021

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.