I would like to clear all inputs, selects and also all hidden fields in a form. Using jQuery is an option if best suited.
What is the easiest way to do this... I mean easy to understand and maintain.
[EDIT]
The solution must not mess with check-boxes (the value must remain, but checked state must be cleared), nor the submit button.
What I am trying to do is a clear button, that clears all the options entered by the user explicitly, plus hidden-fields.
Thanks!
To clear all the input in an HTML form, use the <input> tag with the type attribute as reset.
To clear an input field after submitting:Add a click event listener to a button. When the button is clicked, set the input field's value to an empty string. Setting the field's value to an empty string resets the input.
You can use the reset()
method:
$('#myform')[0].reset();
or without jQuery:
document.getElementById('myform').reset();
where myform
is the id of the form containing the elements you want to be cleared.
You could also use the :input
selector if the fields are not inside a form:
$(':input').val('');
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