[Javascript] function detecting selected radio button option -- with one failure

Bill javascript at unlockedideas.com
Tue Jan 29 11:41:31 CST 2002


Here's one: 

I've got a function that I use to identify which radio button a user selects. It works great if you have more than one radio button in the group.

My problem is that my radio buttons are dynamically generated, and sometimes only one radio button is in the group.

Can someone see what the problem is which my function, get_radio_value()? To make the snippet below work correctly, uncomment the second radio button.

Best,

Bill

<script language=javascript>
<!--
function get_radio_value (radio_array)
{
 var i;
 for (i = 0; i < radio_array . length; ++ i)
  if (radio_array [i] . checked)
   return radio_array [i] . value;
 return null; 
}

function run_choice() {
 document.write( get_radio_value(sel.radiobutton) )
 document.write("<p><input type=button value=Back")
 document.write(" onClick='history.back()'>")
}

//-->
</script>
<form name=sel method=post>
      <input type="radio" name="radiobutton" value="01003">01003
      <!-- input type="radio" name="radiobutton" value="01004">01004 -->

      <p><input type=button name=ok value=OK onClick='run_choice()'>
</form>

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.evolt.org/pipermail/javascript/attachments/20020129/5ee52309/attachment.htm>


More information about the Javascript mailing list