faqts : Computers : Programming : Languages : JavaScript

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

3 of 8 people (38%) answered Yes
Recently 3 of 8 people (38%) answered Yes

Entry

im my form has some text boxes and some dropdown lists if i click reset button it should clear all

Apr 8th, 2008 18:51
ha mo, Dave Clark, krishna g,


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Krishna,
     This function is one way of resetting all form fields:
function ResetAllFields(f)
{
    var ele = f.elements;
    var x, y, len = ele.length;
    for (x=0; x<len; ++x)
    {
        switch (ele[x].type) {
        case 'password':
        case 'text':
        case 'textarea':
            ele[x].value = '';
            break;
        case 'checkbox':
        case 'radio':
            ele[x].checked = false;
            break;
        case 'select-one':
            ele[x].selectedIndex = 0;
            break;
        case 'select-multiple':
            for (y=0; y<ele[x].options.length; ++y)
              ele[x].options[y].selected = false;
            ele[x].selectedIndex = 0;
            break;
        default:
        }
    }
    return false;
}
     This is how you tie it to your form:
<form ...etc...  onreset="return ResetAllFields(this)">
Take care,
Dave Clark
www.DaveClarkConsulting.com
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
http://www.businessian.com
http://www.computerstan.com
http://www.financestan.com
http://www.healthstan.com
http://www.internetstan.com
http://www.moneyenews.com
http://www.technologystan.com
http://www.zobab.com
http://www.healthinhealth.com