Click to See Complete Forum and Search --> : Detecting the selected radio button


mayordc
10-08-2003, 03:56 AM
From a group of radio buttons like gender with two options male and female, how will i determine which one is selected through javascript?

pyro
10-08-2003, 07:50 AM
Since this was a JavaScript question, I moved it to the JavaScript section...

Anyway, here's how I'd do it: (I used a loop to make it more portable)

<script type="text/javascript">
function check() {
for (i=0; i<document.myform.sex.length; i++) {
if (document.myform.sex[i].checked) {
alert (document.myform.sex[i].value);
}
}
}
</script>
</head>
<body>
<form name="myform" action="">
<p><input type="radio" name="sex" value="male">
<input type="radio" name="sex" value="female"><br>
<input type="button" value="Check" onclick="check();"></p>
</form>