Say you are trying to validate that at least one element in a radio group has been selected.

This hacking is because if a radio group only has one element you can’t treat it as an array so if the length is === (points to the the same object) as an undefined variable, numRowSelects, then there must only be one of them otherwise we use numRowSelects to walk the array of radio buttons, total nonsense.

Assume that theForm is the form with the radio group.
Assume that there is at least one element in the array or it all goes pear shaped.
When you get to the end of this oneSelected will be true if one has been picked or false otherwise.
As a side effect, if there is only one, it will be automatically selected.
This was developed using Firefox and also works with IE.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
var oneSelected = false ;
var numRowSelects;
if ( theForm.pi_row_select.length === numRowSelects ) numRowSelects = 1 ; // numRowSelects was undefined - hack
else numRowSelects = theForm.pi_row_select.length;
if ( numRowSelects == 1 )
{
  oneSelected = true
  theForm.pi_row_select.checked = true;
  theForm.pi_row_select.onclick();
}
else
{
 for( var i = 0; i < numRowSelects ; i ++ )
 {
    oneSelected = theForm.pi_row_select[i].checked
//  alert("loop " + oneSelected);
    if( oneSelected ) break
 }
}