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.
Modification of the most-voted answer for the
$(document).ready()
situation:You might find that this is actually easier solved without jQuery.
In regular JavaScript, this is as simple as:
I try to always remember that while we use jQuery to enhance and speed up our coding, sometimes it isn't actually faster. In those cases, it's often better to use another method.
jQuery Plugin
I created a jQuery plugin so I can use it easily anywhere I need it:
So now I can use it by calling:
I find this works well.
I was having the same problem and the post of Paolo helped me out, but I needed to adjust one thing. My form with id advancedindexsearch only contained input fields and gets the values from a session. For some reason the following did not work for me:
If I put an alert after this, I saw the values where removed correctly but afterwards they where replaced again. I still don't know how or why but the following line did do the trick for me:
Clearing forms is a bit tricky and not as simple as it looks.
Suggest you use the jQuery form plugin and use its clearForm or resetForm functionality. It takes care of most of the corner cases.