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






Append Table Row in JQuery Count number of Rows in a Table using JQuery Set Textarea Value using JQuery Change Button Text using JQuery Highlight Alternate Table Row using JQuery Show and Hide Element on Click using JQuery Open Link in New Window using JQuery Get Current Page URL using JQuery Count Number of Words using JQuery Add HTML Element Before Selected Element Using JQuery Add HTML Element After Selected Element Using JQuery Preview Selected Image Using JQuery Convert Image To Base64 String Using JQuery Open Link in New Tab using JQuery Disable Right Click on Website Using JQuery Hide Element when user click outside using JQuery Check Uncheck Checkbox using JQuery Change HTML Element Width On Hover Using JQuery Reset HTML Form Using JQuery - reset() Method and trigger() Method How To Get Value of Selected Radio Button Using jQuery How To Check If HTML Element Has a Child Using JQuery Submit Form If at least one Checkbox is Checked Disable All Form Elements using JQuery