EN
JavaScript - reset form example
0
points
In this article, we would like to show you how to reset form in JavaScript.
Quick solution:
// get form element by id and reset
document.querySelector("#form-id").reset();
Practical example
In this example, we will reset myForm
using reset()
function.
// ONLINE-RUNNER:browser;
<!doctype html>
<html>
<body>
<form id="my-form">
Username: <input type="text"><br>
Password: <input type="password"><br>
<input type="button" value="Reset" onclick="resetForm()">
</form>
<script>
var myForm = document.querySelector('#my-form');
function resetForm() {
myForm.reset();
}
</script>
</body>
</html>