GeekedOut
GeekedOut

Reputation: 17185

Form contents do not get cleared after form submission using jQuery

I have a form that I am working with, and I am trying to clear it after successful submission using something like this:

$(':text, :password, :file, SELECT', '#create_problem').val('');

The id of my form is create_problem and the elements there are a text box and a check box and a text field.

The code above does nothing to help clear the form. Is it not right?

Thanks!

To reproduce the problem: you can visit: http://www.problemio.com and login with the test account:

login: testuser
password: testuser

and try to add a problem to see that the form does not clear.

Upvotes: 0

Views: 133

Answers (2)

Samuel Liew
Samuel Liew

Reputation: 79022

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

Upvotes: 1

Shadow Wizard
Shadow Wizard

Reputation: 66399

Just use the built in reset() method of the form element itself:

$("#MyForm").get(0).reset();

Upvotes: 3

Related Questions