Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Development Tools

Prince Sargbah
Prince Sargbah
2,551 Points

How can I empty the form field after the form is submitted using ajax?

Hi, I'm using Ajax to submit form data to my database. It's working fine. The form submits the data, the database is updated and the success message is display except, I don't want the success message to replace the form. So I'm displaying the success message above the form when the form is submitted. However, because the page is not reloading, the data the user submit in the form stays in the form. One have to refresh the page in order to resubmit the form. How can I change this so when the form is submitted, the old data can be clear out of the form and the form can be ready again for another submission without user refreshing the page.

Thanks

1 Answer

Jeff Lemay
Jeff Lemay
14,268 Points

When your Ajax request succeeds...

document.getElementById("my-form").reset();