Clearing Form Data
A question I often hear is, "How do I clear a form?"
Initially the answer seems very straightforward - a one-liner in jQuery:
$('form :input').val("");
But upon closer examination we find that this is a bad way to solve the problem. When someone says they want to "clear a form" what they really mean is that they want to clear the visible state from all the form fields. With this in mind, the code above is clearly not the right way to get the job done. First, it will blast away the values of hidden inputs, checkboxes and radio buttons. Not good. The values of those fields should not be altered. And second, it does not properly account for select elements. What we need is something smarter. Here's a start:
-
function clearForm(form) {
-
// iterate over all of the inputs for the form
-
// element that was passed in
-
var type = this.type;
-
var tag = this.tagName.toLowerCase(); // normalize case
-
-
// it's ok to reset the value attr of text inputs,
-
// password inputs, and textareas
-
if (type == 'text' || type == 'password' || tag == 'textarea')
-
this.value = "";
-
-
// checkboxes and radios need to have their checked state cleared
-
// but should *not* have their 'value' changed
-
else if (type == 'checkbox' || type == 'radio')
-
this.checked = false;
-
-
// select elements need to have their 'selectedIndex' property set to -1
-
// (this works for both single and multiple select elements)
-
else if (tag == 'select')
-
this.selectedIndex = -1;
-
});
-
};
We can improve upon this function by making it a plugin which can be called on a form element:
-
$.fn.clearForm = function() {
-
// iterate each matching form
-
// iterate the elements within the form
-
var type = this.type, tag = this.tagName.toLowerCase();
-
if (type == 'text' || type == 'password' || tag == 'textarea')
-
this.value = '';
-
else if (type == 'checkbox' || type == 'radio')
-
this.checked = false;
-
else if (tag == 'select')
-
this.selectedIndex = -1;
-
});
-
});
-
};
One last improvement we can make is to allow for the method to be called on forms, form elements, or both:
-
$.fn.clearForm = function() {
-
var type = this.type, tag = this.tagName.toLowerCase();
-
if (tag == 'form')
-
return $(':input',this).clearForm();
-
if (type == 'text' || type == 'password' || tag == 'textarea')
-
this.value = '';
-
else if (type == 'checkbox' || type == 'radio')
-
this.checked = false;
-
else if (tag == 'select')
-
this.selectedIndex = -1;
-
});
-
};
Voila! Now we have a plugin for clearing form fields that can be called like this:
-
$('form').clearForm()
Or like this:
-
$(':input').clearForm()
You can find this same functionality implemented in the jQuery Form Plugin.



August 16th, 2007 at 10:57 pm
There are also the method reset, where we can go back for initial state
$( "form" )[ 0 ].reset();
August 17th, 2007 at 2:55 am
I definitely think that
$('form')[0].reset()is more desirable, as it will respect form defaults and the browser’s native behaviour, as well as avoiding the overhead of looping through all the elements with jQuery selectors.If anything I’d like to see a
$('form').reset()which used the browser’s native behaviour and returned the jQuery object to continue the chain. Perhaps this is one for the core though.August 17th, 2007 at 7:36 am
Estevão and Dave - thanks for bringing up the "reset" method. Reset, of course, is a different action than clearing the form (although they could be the same depending on the form's initial state). I was trying to provide a fool-proof way of clearing form data, regardless of initial state, but I should have mentioned the reset method too! What's nice about the plugin that I presented is that you can call it on a subset of the form (or any input container for that matter). So if you needed to clear only the fields within a "warning" div you could do this:
$('div.warning').clearForm();August 17th, 2007 at 2:26 pm
It seems easiest to use a HTML input element of the type reset, to achieve this functionality.
August 20th, 2007 at 12:19 am
yaph, please read comment #3 above. Mike does a very good job of explaining the difference between what
<input type="reset">does and what his plugin does.August 20th, 2007 at 5:11 am
For special cases, where you only want to reset certain parts of the form this plug-in is useful, but for cases where you want to reset all fields to the initial state a reset button seems the best choice, because it works without JavaScript.
September 1st, 2007 at 3:20 am
Hi,
In some case with Firefox the "reset" method don't reset the form (why ? i don't know ...), so this solution is good in this special case ... Perhaps better if you reset also the 'hidden' fields ;o)
Many thxs for this script.
September 13th, 2007 at 11:54 am
You should mind <select> and <textarea> also, shouldn´t you?
Sascha
September 14th, 2007 at 3:47 pm
@Sascha: The code handles select and textarea elements. Give it a try!
@Cadaeib: I do not advise resetting input elements of type hidden. The values of those elements are usually not meant to ever change so a generic function like the one provided here should leave them alone.
September 17th, 2007 at 4:13 pm
Great stuff- perfect intro into basic forms with JQuery - thanks very much.
November 14th, 2007 at 3:25 am
Hi guys I noticed that when using: $( "form" )[ 0 ].reset(); that trying to read a value in IE fails.
i.e.
test1
if I do $( "form" )[ 0 ].reset(); then call $('#days').val() it returns an empty string
I thought reset should return back to initial state not clear the forms to nothing?
Does the option have to have selected in it???
Regards Robsworld
December 14th, 2007 at 2:24 pm
For some reason, line 7 wasn't working for me (FF2, linux). I had to change it to:
this.setAttribute('value','');
February 14th, 2008 at 10:43 am
Ockham razor ...
$('#ClearFormButton').click(function() {
if(confirm("Clear form?")) {
$("#id_Of_Display_None_Type_Reset_Input").click();
 $(".other_Form_Elements_Not_Cleared_By_Reset_Such_As_Hidden_Inputs").remove();
}
return false;
});
April 25th, 2008 at 6:44 am
I am all about jQuery -- but for reseting a form the safest method is to give your form a name:
Then use
to reset the form.