I want to clear the form fields (form fields has text and checkbox) after submit. To clear the form, I have created a button. And there is separate button for submit.
<input type="reset" name="clearform" id="clearform" value="Clear Form" />
<form id="submit" method="POST" action="">
I have written jQuery code but its not working:
jQuery("#clearform").click(function(){
jQuery("#submit input[type='text'], input[type='checkbox']").each(function() {
this.value = '';
});
});
To clear all form fields after submitting:Add a submit event listener on the form element. When the form is submitted, call the reset() method on the form. The reset method restores the values of the input fields to their default state.
To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.
Try this:
$('#clearform').on('click', function () {
$('#form_id').find('input:text').val('');
$('input:checkbox').removeAttr('checked');
});
One will clear all text inputs. Second will help unchecking checkboxes.
Hope it helps.
There is a simple solution to reset form via jQuery:
$("form").trigger("reset");
try the code shown below to reset a form
$('#submit')[0].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