- Resetting an HTML form with a reset input button
- Reset an HTML form
- What does it actually do?
- How to clear all fields to an empty state
- How to reset the form with Javascript
- Check Out These Related posts:
- Html reset одного input
- Try it
- Value
- Setting the value attribute
- Omitting the value attribute
- Using reset buttons
- A simple reset button
- Adding a reset keyboard shortcut
- Disabling and enabling a reset button
- Validation
- Examples
- Technical summary
- Specifications
- Browser compatibility
- See also
- Found a content problem with this page?
- MDN
- Support
- Our communities
- Developers
- How to Create a Reset Button in HTML
- Community Q&A
Resetting an HTML form with a reset input button
This post covers the HTML reset input button. I know this is very basic HTML but after some recent responses to my «clear a form with jQuery» post I’m beginning to think a lot of web developers must not know how to do a reset with regular old HTML and seem to think you need to use Javascript.
Reset an HTML form
The value=»…» is optional and without it the text on the button will default to «reset» or similar.
What does it actually do?
Clicking the reset button restores the form to its original state (the default value) before the user started entering values into the fields, selecting radio buttons, checkboxes etc.
So if the following field was in the form:
and the user typed in «apple orange banana pear», clicking the reset button will restore the value of the input to «foo bar baz». It does not clear it to an empty state.
How to clear all fields to an empty state
Unfortunately you can’t clear all the fields to an empty state with regular HTML and need to use Javascript instead, which was the post I mentioned in the opening paragraph. I have previously covered how to do this with regular Javascript and also with jQuery.
How to reset the form with Javascript
You could, but in most cases you don’t need to and should just use a regular reset button instead. You can call the form’s reset function in one of the following ways where ‘formname’ is the name of the form or ‘formID’ is the ID of the form:
document.forms['formname'].reset();
document.getElementById('formID').reset();
$('form[name=formname]').get(0).reset();
Check Out These Related posts:
Html reset одного input
elements of type reset are rendered as buttons, with a default click event handler that resets all inputs in the form to their initial values.
Try it
Note: You should usually avoid including reset buttons in your forms. They’re rarely useful, and are instead more likely to frustrate users who click them by mistake (often while trying to click the submit button).
Value
An element’s value attribute contains a string that is used as the button’s label. Buttons such as reset don’t have a value otherwise.
Setting the value attribute
input type="reset" value="Reset the form" />
Omitting the value attribute
If you don’t specify a value , you get a button with the default label (typically «Reset,» but this will vary depending on the user agent):
Using reset buttons
buttons are used to reset forms. If you want to create a custom button and then customize the behavior using JavaScript, you need to use , or better still, a element.
A simple reset button
We’ll begin by creating a simple reset button:
form> div> label for="example">Type in some sample textlabel> input id="example" type="text" /> div> div> input type="reset" value="Reset the form" /> div> form>
Try entering some text into the text field, and then pressing the reset button.
Adding a reset keyboard shortcut
To add a keyboard shortcut to a reset button — just as you would with any for which it makes sense — you use the accesskey global attribute.
In this example, r is specified as the access key (you’ll need to press r plus the particular modifier keys for your browser/OS combination; see accesskey for a useful list of those).
form> div> label for="example">Type in some sample textlabel> input id="example" type="text" /> div> div> input type="reset" value="Reset the form" accesskey="r" /> div> form>
The problem with the above example is that there’s no way for the user to know what the access key is! This is especially true since the modifiers are typically non-standard to avoid conflicts. When building a site, be sure to provide this information in a way that doesn’t interfere with the site design (for example by providing an easily accessible link that points to information on what the site access keys are). Adding a tooltip to the button (using the title attribute) can also help, although it’s not a complete solution for accessibility purposes.
Disabling and enabling a reset button
To disable a reset button, specify the disabled attribute on it, like so:
input type="reset" value="Disabled" disabled />
You can enable and disable buttons at run time by setting disabled to true or false ; in JavaScript this looks like btn.disabled = true or btn.disabled = false .
Validation
Buttons don’t participate in constraint validation; they have no real value to be constrained.
Examples
We’ve included simple examples above. There isn’t really anything more to say about reset buttons.
Technical summary
Specifications
Browser compatibility
BCD tables only load in the browser
See also
Found a content problem with this page?
This page was last modified on Apr 12, 2023 by MDN contributors.
Your blueprint for a better internet.
MDN
Support
Our communities
Developers
Visit Mozilla Corporation’s not-for-profit parent, the Mozilla Foundation.
Portions of this content are ©1998– 2023 by individual mozilla.org contributors. Content available under a Creative Commons license.
How to Create a Reset Button in HTML
wikiHow is a “wiki,” similar to Wikipedia, which means that many of our articles are co-written by multiple authors. To create this article, volunteer authors worked to edit and improve it over time.
This article has been viewed 90,083 times.
Want to give your visitor the ability to clear all form data they entered? This article will give you the code to input the button they’ll need to clear their data.
- Before you can create a reset button, you need to create an HTML form using the tag.
- Use the tag to create a reset button.
- Place your reset button near the top or the bottom of the form so users can find it easily.
Create several options that the user can type in information or click a selection button, or do something.
Create your reset button. Type the tag into the code towards the top and/or bottom of the HTML form.
Save and preview your new adjusted form with the new reset button. Try it out, to see if it works correctly; click a selection or type a value and then click the reset button.
Community Q&A
if you are trying to reset the HTML form’s fields by clicking a button, you should call a javascript function through that button and, in that function, you would be able to access all HTML forms’ input fields and reset them to whatever you want.
Thanks! We’re glad this was helpful.
Thank you for your feedback.
As a small thank you, we’d like to offer you a $30 gift card (valid at GoNift.com). Use it to try out great new products and services nationwide without paying full price—wine, food delivery, clothing and more. Enjoy! Claim Your Gift If wikiHow has helped you, please consider a small contribution to support us in helping more readers like you. We’re committed to providing the world with free how-to resources, and even $1 helps us in our mission. Support wikiHow