Skip to main content

Command Palette

Search for a command to run...

Resetting a Form

Published
2 min read
Resetting a Form
C

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.

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>

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>
function myReset() {
  var form = document.querySelector('form');
  form.reset();
}

Alternatively even easier we can use the following action:

<button type="button" onclick="reset()">JavaScript #2</button>

View these methods on Codepen.

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

A

Thanks for sharing this 👍🏽

1
C

Your welcome, what kind of topics would you like to see more off?

B

Thanks for writing this.

2
C

Thanks for talking the time in reading this :D

1

More from this blog

D

Daily Dev Tips

887 posts

Looking to get into development? As a full-stack developer I guide you on this journey and give you bite sized tips every single day 👊