Skip to main content
Known Participant
Jan 6, 2020 8:31 PM UTC
Answered

Reset a form - need help with javascript

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 is closed to new replies. Start a new post to keep the conversation going.
Lukas Engqvist
Lukas EngqvistCorrect answer
Community Expert
Jan 7, 2020 5:48 AM UTC

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"

19 replies

Participating Frequently
Dec 29, 2020 1:31 AM UTC

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

Translate
try67
Community Expert
Community Expert
Dec 29, 2020 8:57 AM UTC

Sure:

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

Translate
Known Participant
Aug 20, 2021 2:44 PM UTC

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

 

 

Translate
PSURAMAuthor
Known Participant
Jan 7, 2020 1:06 PM UTC

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

Translate
Participant
Jan 7, 2020 1:12 PM UTC
Cancelar
Translate
Lukas Engqvist
Community Expert
Lukas EngqvistCommunity ExpertCorrect answer
Community Expert
Jan 7, 2020 5:48 AM UTC

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"

Translate
Thom Parker
Community Expert
Community Expert
Jan 6, 2020 9:09 PM UTC

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?

Translate
Thom Parker - Software Developer at PDFScriptingUse the Acrobat JavaScript Reference early and often
Known Participant
Sep 10, 2021 9:39 PM UTC

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?

Translate
try67
Community Expert
Community Expert
Sep 10, 2021 9:44 PM UTC

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.

Translate