tags:

views:

908

answers:

2

Hello all,

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

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

from this question:
http://stackoverflow.com/questions/680241/reset-form-with-jquery

Requires jQuery: http://jquery.com

Chacha102
Do this in the onload function. And of course, this code is only applicable if using jQuery. YMMV.
Ricket
What about the preselected checked items that some forms have? Also this doesn't include text areas...
Moak
Wrong sir: See the :input on jquery's documentation: http://docs.jquery.com/Selectors. It match textareas.
Chacha102
I will give you that you would need to recheck and select the preselected or prechecked items.
Chacha102
+4  A: 
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.

Ricket