Click here to Skip to main content
14,298,693 members
Rate this:
Please Sign up or sign in to vote.
Hello Guys, Need a small help from experts here!Here is a form code which I have to integrate in a website
<form class="form-container" id="myForm" autocomplete="off" method="POST" target="hidden_iframe" onsubmit="submitted=true;">

<input type="text" name="entry.95944133" placeholder="Name" pattern="[A-Za-z]+" title="Please enter your name" autofocus required><br>

<input type="email" name="emailAddress" placeholder="Email" pattern="[a-z0-9._%+-]+@ [a-z0-9.-]+\.[a-z]{2,4}$" title="Please enter a valid email address" required><br>

<select name="entry.1343920721" id="package" class="cmb" required><option class="cmb" value="startup">Startup</option><option class="cmb" value="growth">Growth</option></select><br>

<input type="submit" value="Submit">&nbsp;&nbsp;<button type="button" class="btn" onclick="closeForm()">Close</button>

The issue is that the form is not getting cleared after subbmisson. Please help me.

What I have tried:

I have tried using document.getElementById("myForm").reset(); and this.form.reset(); via javascript but no use.
Posted
Updated 5-Sep-19 18:39pm
v2
Comments
MadMyche 18-Jul-19 9:47am
   
Well that is how you would reset the form, the problem must be with how/where you are calling it; which you did not provide.
And what does CSS have to do with this?
Rate this:
Please Sign up or sign in to vote.

Solution 1

Your code works.
Here it is in a plunker so you can click the button and try it:
Plunker example[^]

I've solved it for you. :)
   
v3
Comments
Online Home Services 19-Jul-19 1:42am
   
Thanks for your help
Rate this:
Please Sign up or sign in to vote.

Solution 2

The following code is resetting the form:
Note that form reset() method is called in onsubmit event.
<form class="form-container" id="myForm" autocomplete="off" method="POST" target="hidden_iframe" onsubmit="submitted=true;document.getElementById('myForm').reset();">

<input type="text" name="entry.95944133" placeholder="Name" pattern="[A-Za-z]+" title="Please enter your name" autofocus required><br>

<input type="email" name="emailAddress" placeholder="Email" title="Please enter a valid email address" required><br>

<select name="entry.1343920721" id="package" class="cmb" required><option class="cmb" value="startup">Startup</option><option class="cmb" value="growth">Growth</option></select><br>

<input type="submit" value="Submit">  
<button type="button" class="btn" onclick="document.getElementById('myForm').reset();">Reset</button>

JsFiddle link for this: Edit fiddle - JSFiddle[^]
   
v2
Comments
Online Home Services 19-Jul-19 1:23am
   
I want to reset the form onsbumit event without using the reset button. Can you help me in the same?
Anurag Gandhi 20-Jul-19 1:23am
   
You can call reset method in form's onsubmit as well. I have updated my solution and JS Fiddle link.
Rate this:
Please Sign up or sign in to vote.

Solution 3

Hi...
Your code is working successfully. I tried to run and getting all field cleared after submit the form.
   
Comments
CHill60 6-Sep-19 3:41am
   
Do not post inane comments as solutions! If you want to comment on a post then use the "Have a Question or Comment?" link next to it

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100