Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Requiring a selection in all sets of radio buttons


Message #1 by "Greg Dunn" <greg.dunn@n...> on Mon, 26 Nov 2001 18:06:33
One more thing:

Add the following to your code:

if (frmForm.elements[i].type == "radio")
{

}

This will make sure only radio buttons are asked for their checked state.

HtH

Imar




> Hi Greg,
> 
> Can you post some of the code, especially the HTML page that creates the 
> radio buttons.
> 
> Form.elements will return all form elements: each radio button will be a 
> separate item. No grouping here, or I must be overlooking something here 
> completely. So on the first iteration, var radioSet = form.elements[i];
> will return the first radio button, not an entire group.
> Checking its length will return 1, so "var j=0; j<radioSet.length" will 
> only loop once, for form.elements[0] to be exact. 
> 
> You can check this by adding a unique ID to every HTML element on the 
form.
> Give each radiobutton a unique ID, even if their names are the same.
> Then inside your code loop, add the following statement:
> 
> alert(document.frmTest.elements[i].id);
> 
> You'll see that each item in the form contents is just an item, like a 
> radio button or a submit button, but not a group.
> 
> If you don't want to use the name of the radio button, simply leave it 
out:
> 
> var len = frmForm.elements.length;
> var i = 0;
> for (i = 0 ; i < len ; i++) 
> {
>     if (frmForm.elements[i].checked == true)
>     {
>         // item is checked		}
>     }
> }
> 
> This will also ask Submit buttons, textboxes etc whether they are 
checked 
> or not. They will, of course, always return false, so that shouldn't 
cause 
> you any trouble.
> 
> getElementsByTagName is not supported by older browsers. If I am not 
> mistaken, it was introduced in NN6 and IE4.
> 
> Imar
> 

  Return to Index