homepage Welcome to WebmasterWorld Guest from 54.226.191.80
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Check to see if a radio button was selected
Is there a quick way without having to go through all the radio buttons?
Jeremy_H




msg:1484481
 12:49 am on Mar 3, 2006 (gmt 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




msg:1484482
 3:12 am on Mar 3, 2006 (gmt 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.

Jeremy_H




msg:1484483
 4:19 am on Mar 3, 2006 (gmt 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.

Bernard Marx




msg:1484484
 9:01 am on Mar 3, 2006 (gmt 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);

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved