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

Check All Checkboxes with jQuery Uncheck All Checkboxes with jQuery Toggle Class OnClick using jQuery Remove id from HTML Tag Using jQuery Detect Shift Keypress with jQuery Remove Class from HTML Tag Using jQuery Delete Last List Element with jQuery Delete First List Element with jQuery Highlight Table Row On Hover using JQuery Toggle Button Text with jQuery Uncheck all Radio Buttons using jQuery Select Option By Index with jQuery jQuery Make Textarea Readonly Detect Enter Key Press in Textarea with jQuery Disable Textarea Resize Property with jQuery Clear Textarea Text with jQuery Get Textarea Tag Value in jQuery Count Characters of Textarea in jQuery 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