views:

1872

answers:

8

How can we access the value of a radio button using the DOM?

For eg. we have the radio button as :

<input name="sex" type="radio" value="male">

<input name="sex" type="radio" value="female">

They are inside a form with name form1. When I try

document.getElementByName("sex").value

it returns 'male' always irrespective of the checked value.

+6  A: 

There are a couple of ways.

1. Put an id on each input

<input name="sex" id="sex_male" type="radio" value="male">
<input name="sex" id="sex_female" type="radio" value="female">

Then you can use document.getElementById("sex_male")

2. Use something like PrototypeJS (jQuery works too)

Then you can do something like this:

//This loops over all input elements that have a name of "sex"
$$('input[name="sex"]').each( function(elem) {
  //Do something
});

Or even this to get at just the "male" radio button:

$$('input[name="sex"][value="male"]').each(function(elem) {
  //Do something
});

An easy way to get started with Prototype is include it from Google by adding this between the <head></head> tags of your page:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/prototype/1.6.0.3/prototype.js"&gt;&lt;/script&gt;
Mark Biek
A: 

If you use document.form1.sex, you are returned an array.

document.form1.sex[0] = first radio button

document.form1.sex[1] = second radio button

To check which is checked you need to loop:

whoChecked(document.form1.sex)

funtcion whoChecked(fieldName) {
   for(var x=0;x<fieldName.length;x++) {
     if(fieldName[x].checked) {
        return fieldname[x].value
     }
}
Diodeus
While this approach certainly works, I find it be kind of fragile. There end up being a lot of things that you have to remember to change if you rename your form or change the order of the HTML elements.
Mark Biek
+3  A: 

If you need the selected one, most frameworks support functionality like this:

//jQuery
$("input[name='sex']:checked").val()
eteubert
Surely it should be ‘:checked’? Also note it is possible for neither radio button to be selected in which case the val() call will fail.
bobince
You are right, I updated my message.
eteubert
A: 
var list = document.getElementsByName('sex');
for(var i=0;i<list.length;i++){
   if(list[i].type=='radio' && list[i].checked){
      alert(list[i].value);
      break;
   }
}
Manu
A: 

document.getElementByName("sex").value

You mean getElementsByName('sex')[0].value? (There's no ‘getElementByName’.)

That will of course always pick the first input element with that name — the one whose value is indeed male. You then check to see if it's selected by using the ‘.checked’ property.

For this simple case you can get away with:

var sex= document.getElementsByName("sex")[0].checked? 'male' : 'female';

For the general case you have to loop over each radio input to see which is checked. It would probably be better to get the form object first (putting an id on the form and using document.getElementById is generally better than using the ‘name’-based document.forms collection), and then access form.elements.sex to get the list, in case there are any other elements on the page that have ‘name="sex"’ attributes (potentially other things than form fields).

bobince
+2  A: 

You can get your selected radio's value by this method :

<script>
function getRadioValue()
{
    for (var i = 0; i < document.getElementsByName('sex').length; i++)
    {
     if (document.getElementsByName('sex')[i].checked)
     {
      return document.getElementsByName('sex')[i].value;
     }
    }
}
</script>
Canavar
A: 

If you want the selected one, but don't have a framework handy, you can iterate over the element array looking for whichever one is checked:

for (var i = 0; i < document.form1.sex.length; i++) {
    if (document.form1.sex[i].checked) alert(document.form1.sex[i].value);
}
Jarret Hardie
A: 

Jarret Hardie

Very good, thanks for all guys!