JS function to get value from radio buttons

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

JS function to get value from radio buttons

Post by califdon »

I just ran into a stopper: HTML form with several sets of radio buttons (same name= parameter), on which I want to do some validation logic in JS. I'm finding that I can't refer to the value by the usual DOM syntax, document.formname.elementname.value. Do I have to give each button a distinct id and cycle through with getElementById to see which one is checked? Wow! What a drag! Guess I just never needed to do this before.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: JS function to get value from radio buttons

Post by it2051229 »

or you can get all the "input" tags.. then for each input, check if it's a radio button.. then check if it's selected(or checked) attribute is set to "true", then that's it...

assuming...

Code: Select all

 
<div id='radiogroup'>
    <input type='radio' value='a' />
    <input type='radio' value='b' />
    <input type='radio' value='c' />
</div>
 
then...

Code: Select all

 
 
var inputTags = document.getElementsByTagName("input");
 
for(i = 0; i < inputTags.length; i++)
{
    // i am not sure if it is inputTags[i].checked == true
    // or maybe inputTags[i].selected == true
    // but it's either of the two...
    if(inputTags[i].type == "radio" && inputTags[i].selected == true)
    {
        alert(inputTags[i].value);
    }
}
 
 
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: JS function to get value from radio buttons

Post by califdon »

Thanks. That would help a bit, but I was hoping I could directly query a "name" and determine what value it would have, upon being submitted. Guess not. Your "collection" approach is more elegant than what I was thinking of. Thanks for that.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: JS function to get value from radio buttons

Post by VladSun »

document.getElementsByName($name) is more appropriate here.
Also, you should check somehow if the parent form is the submitting form (in case you have several forms with equally named controls in them )...
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: JS function to get value from radio buttons

Post by califdon »

Thanks also, VladSun. I'm going to take the cheap way out on this one, since it's a special situation, where (once I re-examined the logic) I really only need to know if it's the first radio button or not, in each of the radio button groups, so I can just test whether the first button id is checked or not. In case you're curious, the test page is at http://hiddenvilla.org/test/farmtourtest.php. Click on any "Available" link and you'll see the situation on the next page. The rest of the operation hasn't yet been scripted, but notice the price change when you select different radio buttons.

But I appreciate the insights from both of you, for future reference.
User avatar
it2051229
Forum Contributor
Posts: 312
Joined: Tue Dec 25, 2007 8:34 pm

Re: JS function to get value from radio buttons

Post by it2051229 »

and also... if you know how to use the jQuery javascript library.. it's far more easier... just one line of code and you already get the selected result...
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: JS function to get value from radio buttons

Post by califdon »

it2051229 wrote:and also... if you know how to use the jQuery javascript library.. it's far more easier... just one line of code and you already get the selected result...
You know, I really should start using jQuery, but every time I set aside some time to do that, another crisis emerges. I'll bet I'm the only one that happens to... :?
Post Reply