Results 1 to 5 of 5

Thread: Setting Radio Buttons Value

  1. #1

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236

    Question Setting Radio Buttons Value

    I'm writing a script that has four radio buttons. They each have a value of A, B, C, and D. Now, I am pulling a letter value from a database using CGIREXX code (I don't expect anybody to know any CGIREXX) and I have the value, lets say "B" set to a variable. How do I initially set the default value of the radio button group to equal the radio button with the value of "B"? I read something about a defaultChecked property but I wasn't able to get it to work, if you know about this property and how to set it, or just a way to set the default value dynamically, please let me know. Thanks.


  2. #2
    Hyperactive Member thinktank2's Avatar
    Join Date
    Nov 2001
    Location
    Arctic
    Posts
    272
    At start up.

    Code:
    <html>
    <head>
    <title>Test</title>
    </head>
    
    <body>
     <form name="myform">
      A<INPUT TYPE="radio" NAME="myoption"> 
      B<INPUT TYPE="radio" NAME="myoption" CHECKED>
      C<INPUT TYPE="radio" NAME="myoption">
      D<INPUT TYPE="radio" NAME="myoption">
     </form>
    </body>
    </html>
    At runtime to check A, use
    document.myform.myoption[0].checked = true;

  3. #3

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    that would work, I knew about the CHECKED thing that you can embed in the tag but I want to take a variable, and through JavaScript set it. I don't know which value is going to be checked each time so I need to set it dynamically.

    I could use that document.form.options[x].checked line but I would need to run it through a loop to check if

    document.form.options.value = myVariable

    and if so then set the .checked to true. but isn't there a simplier way. To set a textbox you just say document.form.tBox.value = myVariable.

  4. #4
    Hyperactive Member thinktank2's Avatar
    Join Date
    Nov 2001
    Location
    Arctic
    Posts
    272
    No. there isn't a simple way as far as I know.. u have to loop through all the option box.

  5. #5

    Thread Starter
    Addicted Member TheGoldenShogun's Avatar
    Join Date
    Mar 2001
    Location
    VA/MD... anywhere around the beltway
    Posts
    236
    thanks, that won't be too hard to do. I was just being lazy, trying to find an easier way.

Posting Permissions

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



Click Here to Expand Forum to Full Width