G
Garry Jones
I have a reset function for a user form which clears text fields, resets
drop down boxes to initial values and moves focus to first input field.
However I need to reset radio buttons to their inital value (of zero).
The bit I am stuck with is
if (elements.checked) {
elements.checked = false;
}
This unclicks the radio buttons. However on the actual form its more like
this...
O No thanks
O 1
O 2
O 3
Initially the form loads with "No thanks" already checked. If the user has
now clicked 1, 2 or 3 and uses my reset button to call this function I want
to use this to set all radio buttons to [0] (ie to check "No Thanks" or
whatever the first value [0] is.
This is the entire function, any help greatly appreciated. I need to be able
to address elements[0]
function clearForm(oForm) {
var elements = oForm.elements;
oForm.reset();
for(i=0; i<elements.length; i++) {
field_type = elements.type.toLowerCase();
switch(field_type) {
case "text":
case "password":
case "textarea":
case "hidden":
elements.value = "";
break;
case "radio":
case "checkbox":
if (elements.checked) {
elements.checked = false;
}
break;
case "select-one":
case "select-multi":
elements.selectedIndex = 0;
break;
default:
break;
}
}
elements[0].focus();
}
Garry Jones
Expat in Sweden
drop down boxes to initial values and moves focus to first input field.
However I need to reset radio buttons to their inital value (of zero).
The bit I am stuck with is
if (elements.checked) {
elements.checked = false;
}
This unclicks the radio buttons. However on the actual form its more like
this...
O No thanks
O 1
O 2
O 3
Initially the form loads with "No thanks" already checked. If the user has
now clicked 1, 2 or 3 and uses my reset button to call this function I want
to use this to set all radio buttons to [0] (ie to check "No Thanks" or
whatever the first value [0] is.
This is the entire function, any help greatly appreciated. I need to be able
to address elements[0]
function clearForm(oForm) {
var elements = oForm.elements;
oForm.reset();
for(i=0; i<elements.length; i++) {
field_type = elements.type.toLowerCase();
switch(field_type) {
case "text":
case "password":
case "textarea":
case "hidden":
elements.value = "";
break;
case "radio":
case "checkbox":
if (elements.checked) {
elements.checked = false;
}
break;
case "select-one":
case "select-multi":
elements.selectedIndex = 0;
break;
default:
break;
}
}
elements[0].focus();
}
Garry Jones
Expat in Sweden