I need to obtain the value of a radio button using javascript
I have a radio group called selection
Although Matt's solution works perfectly fine and solves your immediate problem, I would also recommend that you start looking into a JavaScript library, like JQuery, if you will be frequently querying the DOM.
With JQuery, your solution is a one-liner:
var selectedValue = $("input[name=selection]:checked").val();
Use:
function getRadioValue(name) {
var group = document.getElementsByName(name);
for (var i=0;i<group.length;i++) {
if (group[i].checked) {
return group[i].value;
}
}
return '';
}
Application:
var selectedValue = getRadioValue('selection');
Demo:
http://www.jsfiddle.net/tqQWT/