Dynamic Form Question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Geoff

    Dynamic Form Question

    function AddBlock() {
    I have a dynamic form that allows the user to click an 'add' button to
    add extra groups of input fields. These fields are name, phone, and
    type.

    Let's say that a user enters one Point of Contact, then wants to enter
    another by clicking the 'add' button. I want to save the previously
    entered data into an array structure. I'm having trouble with the below
    snippet of code:

    // Save previously entered data here
    var formObj = document.dynoPo c;
    for (i=0,j=0; i<loops; i++) {
    data[j] = formObj.POC_nam e[i].value;
    j++;
    data[j] = formObj.POC_pho ne1[i].value;
    j++;
    data[j] = formObj.POC_typ e[i].value;
    j++;
    }

    What I expect to happen for the very first line of the loop is that the
    line 'data[j] = formObj.POC_nam e[i].value;'
    gets evaluated to 'data0 = formObj.POC_nam e0.value;'. What seems to be
    happening is 'data0 = formObj.POC_nam e.value;'.

    This causes an error because there is no input field named 'POC_name'...
    they're called 'POC_name0, POC_name1, ...POC_name[n] depending on the
    number of times you click the 'add' button.

    Any suggestions:

    Thanks

  • Richard Cornford

    #2
    Re: Dynamic Form Question

    "Geoff" <gtb104gtb@nets cape.net> wrote in message
    news:bnref902ji r@enews2.newsgu y.com...
    <snip>[color=blue]
    >What I expect to happen for the very first line of the loop is
    >that the line 'data[j] = formObj.POC_nam e[i].value;'
    >gets evaluated to 'data0 = formObj.POC_nam e0.value;'.[/color]
    <snip>

    Why do you expect that to happen? That is not how square bracket syntax
    works in any JavaScript context. Look at (and follow the link from):-

    <URL: http://www.jibbering.com/faq/#FAQ4_39 >

    Richard.


    Comment

    • Geoff

      #3
      Re: Dynamic Form Question

      Correct, I was having a brain fart! It works now.

      Richard Cornford wrote:
      [color=blue]
      >"Geoff" <gtb104gtb@nets cape.net> wrote in message
      >news:bnref902j ir@enews2.newsg uy.com...
      ><snip>
      >
      >[color=green]
      >>What I expect to happen for the very first line of the loop is
      >>that the line 'data[j] = formObj.POC_nam e[i].value;'
      >>gets evaluated to 'data0 = formObj.POC_nam e0.value;'.
      >>
      >>[/color]
      ><snip>
      >
      >Why do you expect that to happen? That is not how square bracket syntax
      >works in any JavaScript context. Look at (and follow the link from):-
      >
      ><URL: http://www.jibbering.com/faq/#FAQ4_39 >
      >
      >Richard.
      >
      >
      >
      >[/color]

      Comment

      Working...