...

View Full Version : Setting a hidden field through radio buttons



XtrmelyCanadian
07-11-2003, 05:42 PM
I want to set a hidden field on a form when a user clicks one of 5 radio buttons, here is my code so far....


<INPUT type="hidden" name="q2" value="">
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">5<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='5'"></font></td>
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">4<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='4'" ></font></td>
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">3<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='3'" ></font></td>
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">2<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='2'" ></font></td>
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">1<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='1'" ></font></td>


Right now though i am getting a "document.forms.0.q2" is null or not an object, is the above code actually feesable ??? Any help would be appreciated....

Kor
07-11-2003, 06:13 PM
i see no [B]form[/B} tag around... so, which form[0] are u calling?

Terry
07-11-2003, 06:18 PM
Hi,

The code worked fine for me. I'm venturing a guess that you ommitted the <form> tags. Try something like this:



<html>
<head>
<script type="text/javascript">
function setHidden(val)
{
var el = document.forms[0].q2;

el.value = val;
alert(document.forms[0].q2.value);
}
</script>
</head>
<body>
<form>
<input type="hidden" name="q2" value="">
<table><tr>
<td width="20%">5<input type="radio" name="cat2" value="" onClick="setHidden(5);"></td>
<td width="20%">4<input type="radio" name="cat2" value="" onClick="setHidden(4);"></td>
<td width="20%">3<input type="radio" name="cat2" value="" onClick="setHidden(3);"></td>
<td width="20%">2<input type="radio" name="cat2" value="" onClick="setHidden(2);"></td>
<td width="20%">1<input type="radio" name="cat2" value="" onClick="setHidden(1);"></td>
</tr></table>
</form>
</body>
</html>


The alert is there to check if the value is being set or not. To get rid of of it just comment it out.

// alert(document.forms[0].q2.value);


I'm not sure of your level of javascript so if I sound condescending, its not intentional.

Born and bred Maritimer :thumbsup:,
-Terry

Kor
07-11-2003, 06:18 PM
<form>
<INPUT type="hidden" name="q2" value="">
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">5<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='5'"></font></td>
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">4<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='4'" ></font></td>
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">3<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='3'" ></font></td>
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">2<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='2'" ></font></td>
<td width="20%"><font size="-1" face="verdana, arial, helvetica" class="medtext">1<INPUT type="radio" name="cat2" value="" onClick="document.forms[0].q2.value='1'" ></font></td>
<input type="button" name="Button" value="Button" onclick="javascript:alert(document.forms[0].q2.value)">
</form>

XtrmelyCanadian
07-11-2003, 07:41 PM
I do have the form tag, sorry just forgot to include it. The function worked though, dont know why it wouldnt work in the html tag ??? Thanks Terry and Kor



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum