views:

268

answers:

6

Like the title says, what's the best way in JavaScript to get all radio buttons on a page with a given name? Ultimately I will use this to determine which specific radio button is selected, so another way to phrase the question:

Given a string variable in JavaScript, how can I tell which exact radio button input element (if any) with that string as it's name is currently selected?

I'm not using jQuery. If you want to provide a jQuery answer, go ahead. Someone else might find it useful. But it won't help me and I won't upvote it.

+4  A: 

You can use document.getElementsByName(), passing the name of the radio group, then loop over them inspecting the checked attribute, e.g. something like:

function getCheckedValue( groupName ) {
    var radios = document.getElementsByName( groupName );
    for( i = 0; i < radios.length; i++ ) {
        if( radios[i].checked ) {
            return radios[i].value;
        }
    }
    return null;
}
Rob
This potentially includes `<input type="checkbox" />` with the same name.
Crescent Fresh
Hmm... I was thinking this couldn't work because of the checkboxes. That was kind of the main problem. But I realize now I can add an expression to the if condition to filter them out by tagname, so this should work for me.
Joel Coehoorn
@Joel: I think you meant filter them out by `type`. Tagnames would be the same "input".
Crescent Fresh
Checkboxes with the same `name` attribute are going to screw up the form submission anyways, so I wouldn't worry about it.
Josh Stodola
+3  A: 

Use document.getElementsByName() is the short answer to the question you asked.

However, it may be better to do something like this:

<form name="formFoo">
  Foo: <input type="radio" name="groupFoo" value="foo" checked> <br />
  Bar: <input type="radio" name="groupFoo" value="bar"> <br />
  Baz: <input type="radio" name="groupFoo" value="baz"> <br />
  <input type="submit" >
</form>

Then use the JavaScript:

function getRadioValue(formName, groupName) {
    var radioGroup = document[formName][groupName];
    for (var i=0; i<radioGroup.length; i++)  {
       if (radioGroup[i].checked)  {
       return radioGroup[i].value;
       }
    }
    return null;
}

By doing this, you avoid having to use a function that searches the entire document. It just searches first for the form, then within that form for controls with that same name. The problem here is that if you were to have a checkbox in the middle of the form with the same name, it could be returned instead of the correct radio value. If another type of control was thrown in with the same name, then it could cause an error. Both of these circumstances should probably be considered programmer error, but it wouldn't hurt for the function to be expanded to check for them, at some potential performance loss. Just change the line:

       if (radioGroup[i].checked)  {

to:

       if (radioGroup[i].type=='radio' && radioGroup[i].checked)  {
Joshua D. Boyd
I think document.form.groupFoo[i].value should be document.formFoo.groupFoo[i].value.
Phaedrus
@Phaedrus, I revised the entire function, and actually tested it this time, but you were right.
Joshua D. Boyd
+2  A: 
var options = document.getElementsByName('myRadioButtons');
for(i = 0; i < options.length; i++)
{
    var opt = options[i];
    if(opt.type=="radio")
    {              
        if(opt.checked)
        {
        }                  
    }
}
Phaedrus
+2  A: 

I'll bite for the jQuery answer

var selectedValue = $("input[name='radio_name']:checked").val();
Bob
A: 

$("input[type='radio'][name='xxxxx']:checked").val()

Andy T
A: 
<form name="myForm" id="myForm" action="">  
<input type="radio" name="radioButton" value="c1">Choice 1
<input type="radio" name="radioButton" value="c2">Choice 2
</form>
<script>
var formElements = window.document.getElementById("myForm").elements;
var formElement;
var radioArray = [];

for (var i = 0, j = 0; i < formElements.length; i++) {
    formElement = formElements.item(i);
    if (formElement.type === "radio" && formElement.name === "radioButton") {
        radioArray[j] = formElement;
        ++j;
    }
}
alert(radioArray[0].value);
alert(radioArray[1].value);
</script>