DEV Community

Cover image for Resetting a Form
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

Resetting a Form

Let's look at a function we used to have a lot but somehow seems a bit faded for no good reason!

The form reset option was usually a button at the bottom of your form, to reset the whole input.

I work for a company that uses a lot of pre-population based on cookies, but sometimes this information is not what you want in the inputs so people can reset the whole form.

HTML Reset Method

One method to reset a form a just by using the reset input type and your HTML would look like this;

<form>
  <input type="text" />
  <br /><br />
  <input type="text" />
  <br /><br />
  <input type="text" />
  <br /><br />
  <input type="reset" />
</form>
Enter fullscreen mode Exit fullscreen mode

Try and type something in the fields and press the reset button, this will reset the whole form.

JavaScript Reset Method

Another way to do this is with JavaScript, for instance, after a submit.

Let's add the following button:

<button type="button" onclick="myReset()">JavaScript</button>
Enter fullscreen mode Exit fullscreen mode
function myReset() {
  var form = document.querySelector('form');
  form.reset();
}
Enter fullscreen mode Exit fullscreen mode

Alternatively even easier we can use the following action:

<button type="button" onclick="reset()">JavaScript #2</button>
Enter fullscreen mode Exit fullscreen mode

View these methods on Codepen.

See the Pen Resetting a Form by Chris Bongers (@rebelchris) on CodePen.

Alt Text

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Top comments (2)

Collapse
 
js_bits_bill profile image
JS Bits Bill

Love it! It's easy to overlook the built-in functionality the browser already provides but tips like this are great for reminding you they're there.

Collapse
 
dailydevtips1 profile image
Chris Bongers

Yeah it's not used often, but always a good to keep in mind what we do have and possibly need one day.