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 9 of 9
  1. #1
    New Coder
    Join Date
    Jan 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Need help accessing form elements using variables.

    So i thought i was being clever. I put together some code that would create a HTML form relevant to the user according to previously entered information. Elements in the form are created as needed and named using an array variable. However, i can't figure out how to access the form's information. Below is a very basic example of what i mean:

    Code:
    <html>
    <head>
    <title>test</title>
    <script language="JavaScript">
      
      function MakeForm() 
      {
      for (i = 0; i <= orderCnt-1; i++)  
        {
        document.write("Order " + i + ": <br>")
        document.write("<input type='radio' name='" + orders[i] + "' value='apple' checked> Apple<br>")
        document.write("<input type='radio' name='" + orders[i] + "' value='Peach'> Peach<br>")
        document.write("<input type='radio' name='" + orders[i] + "' value='Pear'> Pear<br><br>")
        }
      }
      
      function ProcessForm(form) 
      {
        alert("Help!  I can't access the form elements by name!")
      }
    
    </script>
    
    </head>
    <body>
    <script language="JavaScript">
      orders = new Array()
      orders[0] = "Jerry"
      orders[1] = "Tom"
      orders[2] = "Lisa"
    
      orderCnt = 3
    </script>
    
    <form name="test">
    <script language="JavaScript">
    MakeForm();
    </script>
    <input type="button" value="submit" onCLick="ProcessForm(this.form);">
    
    </form>
    
    </body>
    </html>
    The first form element should be named "Jerry". However, trying access a value with something like "document.test.Jerry[0].value" causes an error, as does using the array itself (which would be much more useful for checking the form with a loop) like "document.test.orders[0][0].value".

    Can anyone help me understand how I can access form elements which were named using an array variable? Any advice you can offer would be greatly appreciated!

    Thanks in advance,
    -Scott
    Last edited by SheinTao; 07-10-2006 at 11:06 PM.

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,048
    Thanks
    0
    Thanked 251 Times in 247 Posts
    The radio buttons are named differently so they don't form an array. Each radio button is an independent control.

  • #3
    New Coder
    Join Date
    Jan 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    What do you mean? In this case i have 3 sets of 3 radio buttons. All 3 items in a set are named the same, and the 3 sets are named differently. If you play around with the form you'll see that it acts the way it should, as if i had done it with html like:

    Code:
    <input type="radio" name="Jerry" value="apple" checked> Apple<br>
    <input type="radio" name="Jerry" value="peach"> Peach<br>
    <input type="radio" name="Jerry" value="pear"> Pear<br>
    <input type="radio" name="Tom" value="apple" checked> Apple<br>
    <input type="radio" name="Tom" value="peach"> Paech<br>
    <input type="radio" name="Tom" value="pear"> Pear<br>
    <input type="radio" name="Lisa" value="apple" checked> Apple<br>
    <input type="radio" name="Lisa" value="peach"> Peach<br>
    <input type="radio" name="Lisa" value="pear"> Pear<br>
    ... only i can't figure out how to access the values

  • #4
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    document.form1.Jerry.value
    document.form1.Jerry.item(0).value

  • #5
    New Coder
    Join Date
    Jan 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Beagle
    document.form1.Jerry.value
    document.form1.Jerry.item(0).value
    Did you try those? They don't work for me...

  • #6
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    document.form1.Jerry.value doesn't work for me

    document.form1.Jerry.item(0).value does

  • #7
    New Coder
    Join Date
    Jan 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Beagle
    document.form1.Jerry.value doesn't work for me

    document.form1.Jerry.item(0).value does
    Thanks Beagle, but i still can't get it to work. Where am i going wrong?

    Code:
    <html>
    <head>
    <title>test</title>
    <script language="JavaScript">
      
      function MakeForm() 
      {
      for (i = 0; i <= orderCnt-1; i++)  
        {
        document.write("Order " + i + ": <br>")
        document.write("<input type='radio' name='" + orders[i] + "' value='apple' checked> Apple<br>")
        document.write("<input type='radio' name='" + orders[i] + "' value='Peach'> Peach<br>")
        document.write("<input type='radio' name='" + orders[i] + "' value='Pear'> Pear<br><br>")
        }
      }
    
      
      function ProcessForm(form) 
      {
        alert(document.form1.Jerry.item(0).value)
      }
    
    </script>
    
    </head>
    <body>
    <script language="JavaScript">
      orders = new Array()
      orders[0] = "Jerry"
      orders[1] = "Tom"
      orders[2] = "Lisa"
    
      orderCnt = 3
    </script>
    
    <form name="test">
    <script language="JavaScript">
    MakeForm();
    </script>
    <input type="button" value="submit" onCLick="ProcessForm(this.form);">
    
    </form>
    
    
    </body>
    </html>

  • #8
    Senior Coder
    Join Date
    Jul 2005
    Location
    New York, NY
    Posts
    1,084
    Thanks
    4
    Thanked 19 Times in 19 Posts
    your form is named test, not form1

    document.test.Jerry.item(0).value

  • #9
    New Coder
    Join Date
    Jan 2006
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    ahhh, ok... that did it. I tried that before posting, but it must have been the [0] instead of the (0) that was causing the error. Thanks for your time!


  •  

    Posting Permissions

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