Click here to Skip to main content
15,558,557 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello Guys, Need a small help from experts here!Here is a form code which I have to integrate in a website
JavaScript
<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 19: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?

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. :)
 
Share this answer
 
v3
Comments
[no name] 19-Jul-19 1:42am    
Thanks for your help
The following code is resetting the form:
Note that form reset() method is called in onsubmit event.
JavaScript
<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[^]
 
Share this answer
 
v2
Comments
[no name] 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.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900