[Javascript] Form field arrays in IE 5+

Peter Brunone peter at brunone.com
Thu Apr 24 12:02:57 CDT 2003


Sorry; I forgot a crucial line (see below).  That's what I get for cutting and pasting pieces...

---------- Original Message ----------------------------------
From: "Peter Brunone" <peter at brunone.com>

>I could've sworn I've done this before.
>
>   I'm looping through a form's elements, and when I get to a radio button or checkbox, I want to determine whether any options are checked.  Now, as far as I can remember, if you have a radio/checkbox collection with all elements named the same, then you can treat it as an array... BUT, for some reason, IE is still acting as if each of them is a separate entity, which really gives me no way to tie them together.  Below is a sample piece of script I'm using to get the pieces, but the browser refuses to believe that four <input type="radio"> with the same name are really pieces of one collection.
>
>    for(var i=0;i<document.mainForm.elements.length;i++) {

         currentField = document.mainForm.elements[i];

>        if(currentField.type == "text") {
>            if(currentField.value == "") { isValid = 0 }
>            }
>        else if(currentField.type == "radio") {
>            isChecked = 0;
>            j = 0;
>            for(var j=0;j<currentField.length;j++) {                
>                if(currentField[j].checked == true) {isChecked = 1}
>                alert(currentField.value + " " + currentField.checked); // Debugging
>                }
>            if(isChecked == 0) { isValid = 0 }
>            }
>        }


More information about the Javascript mailing list