Connecting Tech Pros Worldwide Forums | Help | Site Map

Dynamic Form Question

Geoff
Guest
 
Posts: n/a
#1: Jul 20 '05
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.dynoPoc;
for (i=0,j=0; i<loops; i++) {
data[j] = formObj.POC_name[i].value;
j++;
data[j] = formObj.POC_phone1[i].value;
j++;
data[j] = formObj.POC_type[i].value;
j++;
}

What I expect to happen for the very first line of the loop is that the
line 'data[j] = formObj.POC_name[i].value;'
gets evaluated to 'data0 = formObj.POC_name0.value;'. What seems to be
happening is 'data0 = formObj.POC_name.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
Guest
 
Posts: n/a
#2: Jul 20 '05

re: Dynamic Form Question


"Geoff" <gtb104gtb@netscape.net> wrote in message
news:bnref902jir@enews2.newsguy.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_name[i].value;'
>gets evaluated to 'data0 = formObj.POC_name0.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.


Geoff
Guest
 
Posts: n/a
#3: Jul 20 '05

re: Dynamic Form Question


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

Richard Cornford wrote:
[color=blue]
>"Geoff" <gtb104gtb@netscape.net> wrote in message
>news:bnref902jir@enews2.newsguy.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_name[i].value;'
>>gets evaluated to 'data0 = formObj.POC_name0.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]

Closed Thread


Similar JavaScript / Ajax / DHTML bytes