Web Development - Quickly Clear All HTML Form Elements Using JavaScript's Reset() Function

While including a reset button (e.g., <input type="reset">) allows the user to clear all the elements on the form, there may also be times when you want to clear all form elements programmatically. At first, you may be tempted to loop through all the elements on a form and clear them one by one--something along the lines of:

  • for (x=0; x <document.form.elements.length;x++) {

Executing this command resets all the elements in Form1. And if your page has more than one form, your code can simply loop through the collection of forms, calling the reset() function as it goes, as in:

  • for (x=0; x <document.forms.length;x++) {
  •     document.forms[x].value = ";
  • }

Go back