Dynamic Form Question

G

Geoff

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.value;
j++;
data[j] = formObj.POC_phone1.value;
j++;
data[j] = formObj.POC_type.value;
j++;
}

What I expect to happen for the very first line of the loop is that the
line 'data[j] = formObj.POC_name.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
 
R

Richard Cornford

What I expect to happen for the very first line of the loop is
that the line 'data[j] = formObj.POC_name.value;'
gets evaluated to 'data0 = formObj.POC_name0.value;'.

<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.
 
G

Geoff

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

Richard said:
What I expect to happen for the very first line of the loop is
that the line 'data[j] = formObj.POC_name.value;'
gets evaluated to 'data0 = formObj.POC_name0.value;'.

<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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top