I have tried $("#client.frm").reset(); but it is not working.So how to reset form via jQuery?
11 Answers
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'
3 Comments
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":
case "number":
case "date":
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;
}
}
}
6 Comments
selectedIndex = -1 trick.)var field_type = elements[i].type.toLowerCase();defaultValues, you could just call reset() on the form object. The code here is to clear the form, rather than reset it.Note, function form.reset() will not work if some input tag in the form have attribute name='reset'
1 Comment
id="reset" changed to id="rst" and all is working fine.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
Comments
trigger form reset, remove checked + selected attributes (triggering reset doesn't remove attributes) and empty input values
$(document).on('click', 'button[data-reset-form]', function(e){
e.preventDefault();
$('form.my-form').trigger('reset')
$('form.my-form').find('[checked]').removeAttr('checked')
$('form.my-form').find('[selected]').removeAttr('selected')
$('form.my-form').find('input').val('');
})
.reset()) will put the values back to the original values in the HTML. For this, see Nick Craver's solution below. "Clear" typically means to set the values back to blank / unchecked / unselected; see MahmoudS solution.