PDA

View Full Version : Radio button problem


mngmd
09-24-2005, 11:54 PM
This should be easy, but I'm a complete newbie.

I've figured out combo boxes, but I want to use radio buttons to allow for selections. I'm using only javascript...

IN the body, I have:

<FORM NAME="test" ONSUBMIT="return setfilen(this)">
<P><INPUT TYPE="RADIO" NAME="cal" VALUE="Nov" CHECKED />November 6 thru December 3<br></P>
<P><INPUT TYPE="RADIO" NAME="cal" VALUE="Nov_" />December 4 thru December 31</P>

In the script itself, I have

function setfilen (form) {
alert (form.cal.value)}

Works fine when there's only one radio button, but as soon as I add a second, I get "Undefined". (This is primitive, I know). What am I doing wrong?

birdbrain
09-25-2005, 02:42 AM
Hi mngmd,

try this, it will work for any number of inputs with the same name...

<script type="text/javascript">
<!--
function setfilen () {
var inps=document.getElementsByTagName('input');
for(i=0;i<inps.length;i++) {
if(inps[i].name=='cal') {
alert (inps[i].value);
}
}
}
//-->
</script>

<form action="#" name="test" onsubmit="return setfilen()">
<div>
<input type="radio" name="cal" value="Nov" CHECKED />November 6 thru December 3<br />
<input type="radio" name="cal" value="Nov_" />December 4 thru December 31</br />
<input type="submit"/>
</div>
</form>


reson for edit....pore speling :)

mngmd
09-25-2005, 12:05 PM
OK.

But how do I get it to tell me which one is checked?

Thanks in advance.

birdbrain
09-25-2005, 01:01 PM
Hi mngmd,
But how do I get it to tell me which one is checked?

The one that is checked will have it's value submitted. :)
The one that is checked has a a small black dot. :)
If that is still insufficient evidence for your needs then
you can have an alert to give you this information. :)

Try this code it will show you all three options...

<script type="text/javascript">
<!--
function setfilen () {
var inps=document.getElementsByTagName('input');
for(i=0;i<inps.length;i++) {
if((inps[i].name=='cal')&&(inps[i].checked==true)) {
alert ('the radio button checked is number '+(i+1)+'\n\n and it\'s value= '+inps[i].value);
}
}
}
//-->
</script>

<form name="test" action="http://www.google.com" method="get"onsubmit="return setfilen()">
<div>
<input type="radio" name="cal" value="Nov" checked="checked" />November 6 thru December 3<br />
<input type="radio" name="cal" value="Nov_" />December 4 thru December 31<br />
<input type="submit"/>
</div>
</form>

I trust that this will satisfy all of your requirements.