I want to reset a form in a JQuery click event function. How to do that ?
You can easily reset all form values using the HTML button using <input type=”reset”> attribute. Clicking the reset button restores the form to its original state (the default value) before the user started entering values into the fields, selecting radio buttons, checkboxes, etc.
Reset or clear a form using JavaScript, with the reset() method. The reset method sets the values of all elements in a form like clicking the reset button.
The reset() method resets the values of all elements in a form (same as clicking the Reset button). Tip: Use the submit() method to submit the form.
If you use uncontrolled form inputs, an input type of reset will reset the form inputs to their original values. Using controlled inputs, you must set each input state variable value to its default values.
At it's simplest: $("form-selector-here")[0].reset()
but also see: Resetting a multi-stage form with jQuery
Note that jQuery isn't necessary for this at all, as $(selector)[0]
obtains the original DOM element. You could also say document.getElementById("myFormId").reset()
.
$("#btn1").click(function(){
$("#form1")[0].reset();
// OR
document.getElementById("form1").reset();
});
This should work:
$("#formid")[0].reset();
try this
$('#FormID').each (function(){
this.reset();
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With