A quick way to clear all form elements on a Web page using JavaScript

Besides the use of a standard HTML form button, there may be times when you want to clear all form elements programmatically. At first, you may be tempted to loop through all the form elements on the page and clear them one by one, something along the lines of:

for (x=0; x < document.form.elements.length; x++) {document.form.elements[x].value = ' ';}

Of course, as you may have noticed, this code would immediately run into difficulty when it encountered, say, a button or some other non-text field-based element. Fortunately, JavaScript provides a quick way to clear element values in a form with the reset() function.

You use this function like so:

document.forms["Form1"].reset()

Executing this command would reset all the elements in Form1.

Source: Ron Wilder
Viewed 6607 times