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

How To Fade in Image with jQuery How To Change image src with jQuery How To Display Message on Mouse Enter and Leave Events How To Scroll Page to the Bottom using jQuery How To Scroll Page to the Top using jQuery Check radio button Dynamically with jQuery Change Background Color on hover with jQuery Hide and Show div using jQuery Get value of input element with jQuery Remove last child of parent element with jQuery Add li tag to the end of list jQuery Delete first row of Table with jQuery Delete last row of Table with jQuery Add first list item using jQuery jQuery Get id of Button jQuery toggle show hide on click Disable Button after Click with jQuery Get Button Text with jQuery jQuery Dynamic li Click Event How To Remove all Spaces in String with jQuery Get Selected file name in jQuery Remove Last Option of Select tag with jQuery Remove First Option from Select tag in jQuery jQuery Capitalize First Letter of String jQuery Check input value length