How to reset (clear) form through JavaScript?
I have tried $("#client.frm").reset();
but it is not working.So how to reset form via jQuery?
Solution 1:
form.reset()
is a DOM element method (not one on the jQuery object), so you need:
$("#client.frm")[0].reset();
//faster version:
$("#client")[0].reset();
Or without jQuery:
document.getElementById("client").reset();
Note: reset() function does not work if form contains any field with attribute:
name='reset'
Solution 2:
You can simply do:
$("#client.frm").trigger('reset')
Solution 3:
Pure JS solution is as follows:
function clearForm(myFormElement) {
var elements = myFormElement.elements;
myFormElement.reset();
for(i=0; i<elements.length; i++) {
field_type = elements[i].type.toLowerCase();
switch(field_type) {
case "text":
case "password":
case "textarea":
case "hidden":
elements[i].value = "";
break;
case "radio":
case "checkbox":
if (elements[i].checked) {
elements[i].checked = false;
}
break;
case "select-one":
case "select-multi":
elements[i].selectedIndex = -1;
break;
default:
break;
}
}
}
Solution 4:
Note, function form.reset()
will not work if some input tag in the form have attribute name='reset'
Solution 5:
The .reset()
method does not clear the default values and checkbox field and there are many more issues.
In order to completely reset check the below link -
http://www.javascript-coder.com/javascript-form/javascript-reset-form.htm