Wrox Home  
Search P2P Archive for: Go

  Return to Index  

javascript thread: Dynamic Forms


Message #1 by "Preeti Sehgal" <preeti@i...> on Fri, 10 Aug 2001 18:53:40
Hi,

I have a form with two radio buttons - Yes and No.
If a user clicks on yes, two more questions will appear, if the user 
clicks on no, two other questions will appear.  I have it working in IE5.0 
but I can't seem to get it to work with Netscape.  When I run it in 
Netscape 6.1 I get the error that cigars is not defined. What do I do?

Thanks,
Preeti

Here is some sample code:

<html>
   <head>
      <script>
         function showMore(more,what) {          //displays more input 
values depending on other values
            if(more) {
               for (i=0;i<what.length; i++) {
                  what[i].style.visibility='visible';
               }                                 //end for(...)
               showAdv=false;
            } else {                             //otherwise
               for (i=0;i<what.length; i++) {
                  what[i].style.visibility='hidden';
               }                                 //end for(...)
               showAdv=true;
            }                                    //end if(more)
         }                                       //end showMore
(boolean,object)
      </script>
   </head>
   <body>
      <form name=test method=post>
         <table>
            <tr>
               <td><b>Do you smoke? <span class=error>*</span></b></td>
               <td><input type='radio' name='smoker' checked value='Y' 
onClick="showMore(false,cigars)">Yes  <input type='radio' 
name='smoker' value='N' onClick="showMore(true,cigars)">No</td>
            </tr>
            <tr>
               <td><div id=cigars style="visibility:hidden"><b>For how 
long?</b></div></td>
               <td><div id=cigars style="visibility:hidden"><select 
name='nsl'><option value='12'>12 months (1 year)
                                                                           
  <option value='24'>24 months (2 years)
                                                                           
  <option value='36'>36 months (3 years)
                                                                           
  <option value='48'>48 months (4 years)
                                                                           
  <option value=''>5 years +
               </select></div></td>
            </tr>
            <tr>
               <td><div id=cigars style="visibility:hidden"><b>Do you 
smoke cigars?</b></div></td>
               <td><div id=cigars style="visibility:hidden"><input 
type='radio' name='cigar' value='Y' onClick="showMore
(true,cmths)">Yes  <input type='radio' name='cigar' checked 
value='N' onClick="showMore(false,cmths)">No</div></td>
            </tr>
            <tr>
               <td><div id=cmths style="visibility:hidden"><b>How many per 
month?</b></div></td>
               <td><div id=cmths style="visibility:hidden"><input 
type='text' name='cmth' maxlength=3 size=3></div></td>
            </tr>
         </table>
      </form>
   </body>
</html>

  Return to Index