How to Reset Form using jQuery with reset() Method?
Last Updated :
08 Oct, 2024
Reset a form means clear all input fields data and returns to their initial default values. It can be useful where a user wants to clear all entered data or you want to reset the form after a successful submission. The reset() method can easily reset the form data after clicking the reset button or successful form submission.
Resetting a Form using JavaScript reset() Method
The reset() method is a built-in JavaScript method that can be used to reset a form. When invoked, this method restores the form's fields to their default values. The reset() method is used with jQuery to reset the form fields.
Syntax
$('selector')[0].reset();
// OR
$('selector').get(0).reset();
Example 1: In this example, we use reset() method to reset the form data. The form will be reset when user click on "Reset Form" button.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Reset a form using jQuery
with reset() method
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
</head>
<body>
<h1 style="color:green">
GeeksforGeeks
</h1>
<h4>
Reset Form using Reset Form
or Form Submit Buttons
</h4>
<form id="d" name="geek">
<table cellspacing="0" cellpadding="3" border="1">
<tr>
<td align="center">Username</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td align="center">Email</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td align="center">Password</td>
<td>
<input type="password" />
</td>
</tr>
</table>
<br>
<input type="submit" value="Submit" />
<button id="resetBtn">
Reset Form
</button>
</form>
<script>
$(document).ready(function () {
$("#resetBtn").click(function (e) {
//$("#d").trigger("reset");
//$("#d").get(0).reset();
e.preventDefault();
$("#d")[0].reset()
});
});
</script>
</body>
</html>
Output
Example 2: In this example we use jQuery to reset a form with ID d when the "Reset Form" button is clicked. It prevents the default behavior and ensures the form returns to its initial state.
HTML
<!DOCTYPE html>
<html>
<head>
<title>
Reset a form using jQuery
with reset() method
</title>
<script src=
"https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js">
</script>
</head>
<body style="margin: 0 0 0 50px;">
<h1>GeeksforGeeks</h1>
<h4>
Reset Form using Reset Form
or Form Submit Buttons
</h4>
<form id="d" name="geek">
<table cellspacing="0" cellpadding="3" border="1">
<tr>
<td>Username</td>
<td>
<input type="text" name="name" />
</td>
</tr>
<tr>
<td>Email</td>
<td>
<input type="email" name="email" />
</td>
</tr>
<tr>
<td>Password</td>
<td>
<input type="password" />
</td>
</tr>
</table>
<br>
<input type="submit" value="Submit" />
<button id="resetBtn">
Reset Form
</button>
</form>
<script>
$(document).ready(function () {
$("#resetBtn").click(function(e) {
//$("#d").trigger("reset");
//$("#d").get(0).reset();
e.preventDefault();
$("#d").get(0).reset()
});
});
</script>
</body>
</html>
Output:

jQuery is an open source JavaScript library that simplifies the interactions between an HTML/CSS document, It is widely famous with it's philosophy of “Write less, do more". You can learn jQuery from the ground up by following this jQuery Tutorial and jQuery Examples.
Similar Reads
How to Refresh a Page using jQuery? Refreshing a page using jQuery allows you to programmatically reload the current webpage, ensuring updated content or resetting the page's state. It can be done using location.reload() method.Table of ContentUsing location.reload() MethodUsing history.go(0)Using location.replace() with Current PageR
3 min read
How to reset selected value to default using jQuery? Resetting a selected value to its default in a dropdown or select element is a common requirement in web development to enhance user experience. jQuery, a popular JavaScript library, simplifies this task by providing easy-to-use methods for manipulating DOM elements. By using jQuery, you can quickly
3 min read
How to stop a form submit action using jQuery ? In this article, we will learn how to stop a form submit action using jQuery. By default, the HTML form submits automatically. Submitting automatically leads to reloading the whole page again and again. Therefore for performing any operation, we have to prevent its default submission. Given a form,
3 min read
How to submit a form or a part of a form without a page refresh using jQuery? How to prevent an HTML form to submit?When submitting a form, clicking the submit button usually navigates to a new route as specified in the form's action attribute. However, this can be done in the background using event.preventDefault(), which blocks the default event behavior. By attaching an on
2 min read
jQuery UI Accordion refresh() Method jQuery UI consists of GUI widgets, visual effects, and themes implemented using HTML, CSS, and jQuery. The jQuery UI is great for building UI interfaces for the webpages. The jQuery UI Accordion refresh() method is used to refresh the widget. It is used to process any headers and panels that are ad
2 min read