To reset all form fields using jQuery, call the native HTMLFormElement.prototype.reset() method on the underlying DOM element inside the matched jQuery collection (such as $form[0].reset() or $form.each((_, formEl) => { if (typeof formEl.reset === "function") formEl.reset(); })), which restores every form control back to its initial default value (defaultValue / defaultChecked / default <option selected>)—and when you instead need to clear fields to blank/unchecked or reset dynamic/custom controls (select2, hidden inputs, custom state), explicitly clear values, uncheck checkboxes/radios, reset <select> indices, and clear validation or aria-invalid states cleanly.
1. Restoring Default Form Values via Native .reset() in jQuery
jQuery does not have a built-in .reset() method on jQuery collections—calling throws a TypeError: $form.reset is not a function. Access the DOM element(s) via .each() (or [0] for a single form) and invoke formEl.reset():
// Reset one or more matched HTML forms back to their initial default values
function resetJqueryForms($form) {
if (typeof $ === "undefined" || !$form || $form.length === 0) {
throw new TypeError("Expected a non-empty jQuery form collection.");
}
let resetCount = 0;
try {
$form.each((_, element) => {
if (element && typeof element.reset === "function") {
element.reset();
resetCount += 1;
}
});
if (resetCount === 0) {
throw new Error("No matched element supports native form .reset().");
}
return resetCount;
} catch (error) {
throw new Error(`Failed to reset form(s): ${error.message}`);
}
}
// Example usage:
// const $signupForm = $("#signup-form");
// resetJqueryForms($signupForm);
.reset()restores initial HTML defaults, not necessarily empty strings: If an<input value="guest">hasvalue="guest"in HTML,.reset()sets it back to"guest", not""..reset()does not firechangeorinputevents automatically: Notify dependent UI listeners or state handlers explicitly if your form reacts to those events.
2. Clearing All Fields to Blank / Unchecked State Explicitly
When you want to blank out text/numeric inputs, uncheck checkboxes and radio buttons, and clear <select> selections regardless of initial markup attributes, iterate over controls inside the form and reset each control type safely:
// Clear all inputs, textareas, and selects inside a jQuery form container
function clearJqueryFormFields($container, { triggerChange = false } = {}) {
if (typeof $ === "undefined" || !$container || $container.length === 0) {
throw new TypeError("Expected a non-empty jQuery container.");
}
if (typeof triggerChange !== "boolean") {
throw new TypeError("Expected triggerChange to be a boolean.");
}
try {
const $fields = $container.find("input, textarea, select");
$fields.each((_, el) => {
if (!el) {
return;
}
const $el = $(el);
const tagName = el.tagName.toLowerCase();
const inputType = tagName === "input" ? String(el.type || "").toLowerCase() : "";
if (inputType === "button" || inputType === "submit" || inputType === "reset" || inputType === "image") {
return;
}
if (inputType === "checkbox" || inputType === "radio") {
el.checked = false;
} else if (tagName === "select") {
if (el.multiple) {
Array.from(el.options).forEach((opt) => {
opt.selected = false;
});
} else {
el.selectedIndex = -1;
}
} else {
el.value = "";
}
if (triggerChange) {
$el.trigger("change");
}
});
return $fields.length;
} catch (error) {
throw new Error(`Failed to clear form fields: ${error.message}`);
}
}
// Example usage:
// clearJqueryFormFields($("#filter-form"), { triggerChange: true });
- Skip
submit,reset,button, andimageinputs: Clearing.valueon submit/button controls is unnecessary and can strip button labels. - Avoid resetting
disabledor read-only audit fields unless intended: Filter selectors (e.g.,:not([data-preserve="true"])) when certain fields should stay intact.
3. Resetting Validation States, Errors, and Focus Safely
A complete form reset in real applications also clears error messages, validation classes / aria-invalid attributes, and inline style overrides so stale errors do not linger after clearing inputs:
// Reset form controls and clear validation messages/attributes cleanly
function resetFormWithValidationState($form, { clearToBlank = false } = {}) {
if (typeof $ === "undefined" || !$form || $form.length === 0) {
throw new TypeError("Expected a non-empty jQuery form collection.");
}
try {
if (clearToBlank) {
clearJqueryFormFields($form, { triggerChange: false });
} else {
resetJqueryForms($form);
}
$form.find(".is-invalid, .error, .has-error").removeClass("is-invalid error has-error");
$form.find("[aria-invalid='true']").attr("aria-invalid", "false");
$form.find(".field-error, .error-message").text("").hide();
return true;
} catch (error) {
throw new Error(`Failed to reset form and validation state: ${error.message}`);
}
}
// Example usage:
// try {
// resetFormWithValidationState($("#checkout-form"), { clearToBlank: false });
// } catch (error) {
// console.error(`${error.name}:${error.message}`);
// }
- Guard against
null/ empty jQuery collections: Always check$form &&$form.length > 0before operating on DOM nodes. - Avoid calling
.reset()inside a form’s ownresetevent listener without a guard: Can cause recursive resets or unexpected side effects.
