3

I have a simple form, lets say it takes an email address. Once the form is submitted a message stack notifies the user that their address has been submitted successfully. The issue is that after submitting the address the form field with the email still contains the email address that the user typed in, how would I reset this field? Would I have to use JavaScript for this?

Thank you

2 Answers 2

5
window.onload = function () {
    for(var f in document.getElementsByTagName("form"))
        f.reset();
}

This would certainly work, assuming you don't have another onload event already set. If you do, just add the inner bit (lines 2 and 3) to it. And of course this is standard JavaScript, it'll work no matter the framework.

1
$(':input','#myform')
 .not(':button, :submit, :reset, :hidden')
 .val('')
 .removeAttr('checked')
 .removeAttr('selected');

from this question:
Resetting a multi-stage form with jQuery

Requires jQuery: http://jquery.com

4
  • Do this in the onload function. And of course, this code is only applicable if using jQuery. YMMV.
    – Ricket
    Aug 5, 2009 at 0:01
  • What about the preselected checked items that some forms have? Also this doesn't include text areas...
    – Moak
    Aug 5, 2009 at 3:57
  • Wrong sir: See the :input on jquery's documentation: docs.jquery.com/Selectors. It match textareas. Aug 5, 2009 at 4:07
  • I will give you that you would need to recheck and select the preselected or prechecked items. Aug 5, 2009 at 4:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.