You can Reset HTML Form Using JQuery with help of trigger() method or by converting JQuery element to JavaScript Object to use form reset() method.
Table of Contents
Jquery doesn't have any method to directly reset the HTML form, instead this can be done using pure JavaScript. In JavaScript reset() method is used to reset the values of all elements of HTML form.
This works exactly like clicking the reset button of form. The syntax of reset() method is shown below.
Syntax of reset() Method
document.getElementById("FormId").reset();
JQuery trigger() Method
JQuery trigger() method is used to trigger the specified default events. trigger() method can also trigger the JavaScript reset() method, look at the example below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Reset HTML Form Using JQuery</title>
</head>
<body>
<form action="" method="post" id="mainForm">
<label>Name:</label>
<input type="text" name="name">
<br><br>
<label>Class:</label>
<input type="text" name="class">
<br><br>
<label>Age:</label>
<input type="number" name="age">
<input type="submit" value="Submit">
</form>
<br>
<button type="button" id='reset-btn'>Form Reset</button>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
$(document).ready(function(){
$("#reset-btn").click(function(){
$("#mainForm").trigger("reset");
});
});
</script>
</body>
</html>
Demo
reset() method
Another way of using JavaScript reset method is by converting the Jquery element to a JavaScript object. This can be done in two ways as shown below.
$(selector).get(0).reset()
$(selector)[0].reset()
get() method is used to access the DOM elements under specific jQuery object.
Previous form can be reset using this method as demonstrated in example below.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Reset HTML Form Using JQuery</title>
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script>
function resetForm()
{
$("#mainForm1")[0].reset()
}
</script>
</head>
<body>
<form action="" method="post" id="mainForm1">
<label>Name:</label>
<input type="text" name="name">
<br><br>
<label>Class:</label>
<input type="text" name="class">
<br><br>
<label>Age:</label>
<input type="number" name="age">
<input type="button" value="Reset" onclick='resetForm()'>
</form>
</body>
</html>