Something similar to $("#formId").reset()
will not clear form items that have had their defaults set to something other than ""
. One way this can happen is a previous form submission: once a form has been submitted reset()
would "reset" form values to those previously submitted which will likely not be ""
.
One option to clear all forms on the page, is to call a function such as the following, executing it simply as clearForms()
:
function clearForms()
{
$(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
$(':checkbox, :radio').prop('checked', false);
}
If you want to reset a specific form, then modify the function as follows, and call it as clearForm($("#formId"))
:
function clearForm($form)
{
$form.find(':input').not(':button, :submit, :reset, :hidden, :checkbox, :radio').val('');
$form.find(':checkbox, :radio').prop('checked', false);
}
When I originally came to this page I needed a solution that takes into account form defaults being changed and is still able to clear all input items.
Note that this will not clear placeholder
text.