tags:

views:

42

answers:

4

I am currently using this code but it does return the selected radio button value

var mailcopy = document.getElementById('mailCopy').value; 

Please tell any other possible way to get currently selected radio button value using Javascript

A: 

Use the element.checked property.

Francisco Soto
thank u for your reply . can u explain in clear
Meena
A: 
var mailcopy = document.getElementById('mailCopy').checked; 

if(mailcopy==true)
{
  alert("Radio Button Checked");
}
else
{
  alert("Radio Button un-Checked");
}
KhanZeeshan
The question is looking to get the currently selected button, not find out if a specific button is checked.
David Dorward
A: 

guess you trying to find the selected radio button inside a group of radiobuttons.. are you using any framework? else refer: http://www.breakingpar.com/bkp/home.nsf/0/CA99375CC06FB52687256AFB0013E5E9

Ravindra Sane
+2  A: 

Radio buttons come in groups which have the same name and different ids, one of them will have the checked property set to true, so loop over them until you find it.

function getCheckedRadio(radio_group) {
    for (var i = 0; i < radio_group.length; i++) {
        var button = radio_group[i];
        if (button.checked) {
            return button;
        }
    }
    return undefined;
}
var checkedButton = getCheckedRadio(document.forms.frmId.elements.groupName);
if (checkedButton) {
    alert("The value is " + checkedButton.value);
}
David Dorward