I have a form with a standard reset button coded thusly:
<input type="reset" class="button standard" value="Clear" />
Trouble is, said form is of the multi-stage sort, so if a user fills out a stage & then returns later, the 'remembered' values for the various fields won't reset when the Clear button is clicked.
I'm thinking that attaching a jQuery function to loop over all the fields and clear them 'manually' would do the trick. I'm already using jQuery within the form, but am only just getting up to speed & so am not sure how to go about this, other than individually referencing each field by ID, which doesn't seem very efficient.
TIA for any help.
document.getElementById('frm').reset()
I made a little improvement on Paolo Bergantino's original answer
In this way, I can pass any context element to the function. I am able to reset the entire form or only a certain set of fields, for example:
Plus, the default values of the inputs are retained.
I made a slight variation of Francis Lewis' nice solution. What his solution doesn't do is set the drop-down selects to blank. (I think when most people want to "clear", they probably want to make all values empty.) This one does it with
.find('select').prop("selectedIndex", -1)
.From http://groups.google.com/group/jquery-dev/msg/2e0b7435a864beea:
setting
myinput.val('')
might not emulate "reset" 100% if you have an input like this:Eg calling
myinput.val('')
on an input with a default value of 50 would set it to an empty string, whereas callingmyform.reset()
would reset it to its initial value of 50.I usually do:
or
A method I used on a fairly large form (50+ fields) was to just reload the form with AJAX, basically making a call back to the server and just returning the fields with their default values. This made is much easier than trying to grab each field with JS and then setting it to it's default value. It also allowed to me to keep the default values in one place--the server's code. On this site, there were also some different defaults depending on the settings for the account and therefore I didn't have to worry about sending these to JS. The only small issue I had to deal with were some suggest fields that required initialization after the AJAX call, but not a big deal.