Welcome to WebmasterWorld Guest from 54.162.94.15

Forum Moderators: open

Message Too Old, No Replies

Check to see if a radio button was selected

Is there a quick way without having to go through all the radio buttons?

     
12:49 am on Mar 3, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 12, 2005
posts:411
votes: 0


I was reading that "Unfortunately it's not possible to see at once which radio button in a group the user has checked. You need to go through all radio's and see which one's checked property is true."

I was wondering if anybody knew if it was possible to check to see if there was at least a selection made (not which one) without having to go through all the radio buttons?

something like this:
if(document.form.group1.value!=""){};

gph

3:12 am on Mar 3, 2006 (gmt 0)

Full Member

10+ Year Member

joined:Jan 31, 2002
posts:285
votes: 0


I assume you have lots of radio buttons and don't want to loop through them.

There isn't any group property I'm aware of. You could set a global variable to default false then true from each radio.

4:19 am on Mar 3, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Aug 12, 2005
posts:411
votes: 0


Thanks gph for your reply,

Yeah, I have lots of radio buttons for this form I'm constructing, and I'm trying to make my code as efficient as possible.

I did figure out something below that works:

...
else if(!(document.q.q316[0].checked¦¦document.q.q316[1].checked¦¦document.q.q316[2].checked¦¦document.q.q316[3].checked¦¦document.q.q316[4].checked)){alert("Please select field 316.");return false}
...

Now I'm just wondering if I can do something along the lines of this:

...
else if(!(document.q.q316[0-4].checked)){alert("Please select field 316.");return false}
...

Anyone know if something like this would be possible? Thanks.

9:01 am on Mar 3, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
posts:2047
votes: 0


I did figure out something below that works:

else if(!(document.q.q316[0].checked¦¦document.q.q316[1].checked¦¦document.q.q316[2].checked¦¦document.q.q316[3].checked¦¦document.q.q316[4].checked)){alert("Please select field 316.");return false}

IMHO, that's a bits nuts. It is isn't efficient is any sense - speed or amount of code.

document.q.q316[0-4].checked

Not possible.

Why not just loop the buttons?


function getRadioSelection(group)
{
for(var k=0;k<group.length;k++)
if(group[k].checked)
return group[k].value;
}
...
getRadioSelection(document.q.q316);
 

Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members