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 6 of 6
  1. #1
    New Coder
    Join Date
    Jan 2004
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts

    javascript reference to form elements with [] in name

    Javascript throws an error when I try to reference a form field with [] in the name,

    like formname.somefield[].options[]

    Some forms have form name="somefield[]" but when I try to reference it that I can't because thye [] produces a script error. Is there anyway to reference this field by name? Using getelementbyid is not an option here...

  • #2
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    try this:
    Code:
    formname["somefield[]"].options[]

  • #3
    New Coder
    Join Date
    Jan 2004
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That's calling a form with the name, not an element. I tried document.forms[0].["fieldname[]"][0].anything, but that did not work.

    document.forms[0].fieldname[][0].anything - doesn't work
    document.forms[0].fieldname[0].anything - doesn't work
    document.forms[0].fieldname[].anything - doesn't work

    Either it's an invalid name or it's a syntax error. Both ways I have no way of getting the element by its name in javascript.

  • #4
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Here's a few variants to try

    PHP Code:
    <form name="f1">
    <
    input type="text" name="t[]" value="1">
    <
    input type="text" name="t[2]" value="2">
    <
    input type="text" name="t[]3" value="3">
    </
    form>



    <
    script>

    test1=document.forms[0]["t[]"].value
    test2
    =document.forms[0]["t[2]"].value
    test3
    =document.forms["f1"]["t[]3"].value

    test4
    =document.forms[0][0].name
    test5
    =document.forms[0].elements[1].name

    alert
    (test1+"\n"+test2+"\n"+test3+"\n"+test4+"\n"+test5)

    </script> 
    Last edited by Mr J; 12-22-2005 at 10:53 PM.
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.

  • #5
    Regular Coder
    Join Date
    Jul 2005
    Posts
    116
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This works in Firefox and IE.
    Code:
    <html>
     <body>
      <form name="formname">
       <input name="somefield[]"/>
      </form>
      <input type="button" value="Get Value" onclick="alert(document.forms[0]['somefield[]'].value)"/>
     </body>
    </html>
    BTW, my earlier example assumed you were trying to access the form by name due to the info you posted. It should have worked if you tried it like this:
    Code:
    document.formname["somefield[]"].options[]

  • #6
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    I prefer:

    Code:
    document.forms['formname'].elements['elementname[]'].options
    Basscyst
    Helping to build a bigger box. - Adam Matthews


  •  

    Posting Permissions

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