Reset HTML Form Using JQuery - reset() Method and trigger() Method

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.

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>

Demo






Change value of href attribute of link using jQuery Check If Input Field is Empty in jQuery jQuery Select All HTML Elements Whose ID Start With Same String Count Checked Checkboxes of Specific Form with JQuery Enable and Disable Button using JQuery Add Class to HTML Tag On Click Using Jquery Change Class of HTML Tag Using JQuery Display Current Date and Time Using HTML and JQUERY Detect Change in Text Input using JQuery Refresh a Page With JQuery Execute a Function on Hover Using JQuery Open Select File Dialog Box Using JQuery Display Message When File Is Selected Using JQuery Get Class of Clicked Element Using JQuery Remove White Spaces from Start and End of String using JQuery First Digit must be 0 using JQuery OnClick Close and Open Effect using JQuery Display clicked HTML Tag Name using JQuery Delay a Function Execution using JQuery Get ID of Clicked Element using JQuery