I want to clear all input and textarea fields in a form. It works like the following when using an input button with the reset
class:
$(\".reset
Simple but works like a charm.
$("#form").trigger('reset'); //jquery
document.getElementById("myform").reset(); //native JS
$('form[name="myform"]')[0].reset();
Add hidden reset button as follows
<input id="resetBtn" type="reset" value="reset" style="display:none" />
// Call reset buttons click event
// Similar to ClearInputs('resetBtn');
function ClearInputs(btnSelector) {
var btn = $("#" + btnSelector);
btn.click();
}
if you use selectors and make values to empty values, it is not resetting the form, it's making all fields empty. Reset is to make form as it was before any edit actions from user after the load of form from server side. If there is an input with name "username" and that username was prefilled from server side, most of solutions on this page will delete that value from input, not reset it to the value how it was before user's changes. If you need to reset the form, use this:
$('#myform')[0].reset();
if you need not to reset the form, but fill all inputs with some value, for example empty value, then you can use most of the solutions from other comments.
If you want to empty all input boxes irrespective of its type then it's a minute step by
$('#MyFormId')[0].reset();
$('form').submit(function() {
var el = $(this);
$('<button type="reset" style="display:none; "></button>')
.appendTo(el)
.click()
.remove()
;
return false;
});