JS function to get value from radio buttons
Moderator: General Moderators
JS function to get value from radio buttons
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.
Re: JS function to get value from radio buttons
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...
then...
assuming...
Code: Select all
<div id='radiogroup'>
<input type='radio' value='a' />
<input type='radio' value='b' />
<input type='radio' value='c' />
</div>
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);
}
}
Re: JS function to get value from radio buttons
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.
Re: JS function to get value from radio buttons
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 )...
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
Re: JS function to get value from radio buttons
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.
But I appreciate the insights from both of you, for future reference.
Re: JS function to get value from radio buttons
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...
Re: JS function to get value from radio buttons
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...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...