Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Created June 30, 2023 03:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/43ba3895dfdf5cf4bd00d81fa8740f03 to your computer and use it in GitHub Desktop.
Save code-boxx/43ba3895dfdf5cf4bd00d81fa8740f03 to your computer and use it in GitHub Desktop.
Reset HTML Form After Submit

RESET HTML FORM AFTER SUBMIT

https://code-boxx.com/reset-html-form-after-submission/

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

<!DOCTYPE html>
<html>
<head>
<title>No Autocomplete</title>
</head>
<body>
<form method="post" autocomplete="off">
Name: <input type="text" name="user-name" required>
Email: <input type="email" name="user-email" required>
<input type="submit" value="Go!">
</form>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<title>Reset Form After Submit</title>
<script src="2B-reset.js"></script>
</head>
<body>
<form id="my-form" autocomplete="off" onsubmit="return process()">
<!-- (A) TAKE NOTE: THIS WILL RESET TO "JOHN DOE", NOT EMPTY. -->
Name: <input type="text" name="name" value="John Doe" required>
<!-- (B) THIS WILL REVERT TO BLANK -->
Email: <input type="email" name="email" required>
<input type="submit" value="Go!">
</form>
</body>
</html>
function process () {
// (A) GET FORM DATA
var form = document.getElementById("my-form"),
data = new FormData(form);
// (B) AJAX SEND FORM
// NOTE: USE HTTP://. NOT FILE://.
fetch("x-dummy.txt", { method: "POST", body: data })
.then(res => res.text())
.then(txt => {
console.log(txt);
form.reset(); // reset form
});
// (C) STOP DEFAULT FORM SUBMIT/PAGE RELOAD
return false;
}
<!DOCTYPE html>
<html>
<head>
<title>Reset Form After Submit</title>
<script src="3B-reset-part.js"></script>
</head>
<body>
<form id="my-form" autocomplete="off" onsubmit="return process()">
Email: <input type="email" name="email" required>
Password: <input type="password" id="user-password" name="password" required>
Confirm Password: <input type="password" id="confirm-password" name="cpassword" required>
<input type="submit" value="Go!">
</form>
</body>
</html>
function process () {
// (A) GET FORM DATA
var data = new FormData(document.getElementById("my-form"));
// (B) AJAX SEND FORM
fetch("x-dummy.txt", { method: "POST", body: data })
.then(res => res.text())
.then(txt => {
console.log(txt);
document.getElementById("user-password").value = ""; // reset password
document.getElementById("confirm-password").value = ""; // reset confirm password
});
// (C) STOP DEFAULT FORM SUBMIT/PAGE RELOAD
return false;
}
HORRAY, IT WORKS!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment