Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Apr 2003
    Location
    Edmonton, Alberta
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Setting a hidden field through radio buttons

    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....

  • #2
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    i see no [B]form[/B} tag around... so, which form[0] are u calling?
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #3
    New Coder
    Join Date
    Sep 2002
    Location
    Moncton, N.B., Canada
    Posts
    69
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hi,

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

    Code:
    <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 ,
    -Terry

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    <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>
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*

  • #5
    New Coder
    Join Date
    Apr 2003
    Location
    Edmonton, Alberta
    Posts
    15
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •