Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

checking if any radio button is checked 1

Status
Not open for further replies.
Apr 27, 1999
705
US
Hello,

I have multiple radio buttons, all named the same but with different values. I want to check and make sure at least one has been selected. This works for more than one radio button, but if there is only one radio button, it doesn't work.

Here is the code.

function getrdo()

{
var rdo = 'no';
for (var i=0; i < document.usrform.deviceid.length; i++)
{
if (document.usrform.deviceid.checked)
{
var devid = document.usrform.deviceid.value;
rdo = 'yes';
}

}

Any ideas?
 
Probably because when there's only one radio button the length attribute is undefined. Arrays and strings have lengths. A group of radio buttons is an array therefore it has a length. A single radio button does not beaue its neither a string nor an array.

A simple IF statement can tell you whether there is more than one or not by telling you if length is defined or not.
And you can act accordingly.
Code:
if(document.usrform.deviceid.length){
[gray]
 var rdo = 'no';
     for (var i=0; i < document.usrform.deviceid.length;i++)
...
[/gray]
}
else{
 if(document.usrform.deviceid.checked){
    var devid = document.usrform.deviceid.value;
           rdo = 'yes';
 }
}

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Behind the Web, Tips and Tricks for Web Development.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top