#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    488
    Rep Power
    12

    Form field array??


    Hi all!

    I have a several duplicate form field names like below "car[]". Let's say if I want to input the value "Hello" in the one of them how can I do this? I will be using a javascript function where I will pass a number such as "2". So I would like the function to input the vaule "hello" in the 2nd instance of the "car[]" variable in my form "form". How can I do this?


    <form name="form" method="post">
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    </form>
  2. #2
  3. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,158
    Rep Power
    183
    Code:
    <script>
    function insertText(text2insert,key)
    {
    document.getElementsByTagName("form")[0].getElementsByTagName("input")[key-1].value = text2insert;
    }
    </script>
    
    <form name="form" method="post">
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <input type="button" value="Insert Text" onclick="insertText('Hello','2')"/>
    </form>
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Posts
    488
    Rep Power
    12
    Hi,
    There are other form fields inbetween the "car[]" fields. I forgot to mention that. I just stripped down my code so it would be easier to read. I really need the function to only look at the "car[]" name fields and find the instance. Can you help?
  6. #4
  7. A Not To Shabby Code Smurf
    Devshed Beginner (1000 - 1499 posts)

    Join Date
    Aug 2008
    Posts
    1,158
    Rep Power
    183
    You can just adapt it a little bit then:

    Code:
    <script>
    function insertText(text2insert,formKey,inputKey)
    {
    document.getElementsByTagName("form")[formKey-1].getElementsByTagName("input")[inputKey-1].value = text2insert;
    }
    </script>
    
    <form>
    <input type="text"/>
    </form>
    
    <form name="form" method="post">
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <table > <tr><td> CAR #<br><input type='text' class='field' name='car[]' size='1'></td></tr></table>
    <input type="button" value="Insert Text" onclick="insertText('Hello','2','2')"/>
    </form>
    
    <form>
    <input type="text"/>
    </form>
    Where the "formKey" parameter is a number representation of the form location; in relation to the element's page position (from top to bottom).

    Comments on this post

    • nikko50 agrees

IMN logo majestic logo threadwatch logo seochat tools logo