0

I have a webpage that based on user input populates with form fields, as done by the following:

        function Go2() {
            var loans = document.getElementById('count').value;
            var content = document.getElementById('stage3').innerHTML;
            content = '<TABLE Width="100%">'
                +'<TR>'
                +'<TD Style="font-weight:bold;" Width="30%">Customer Name</TD>'
                +'<TD Style="font-weight:bold;" Width="30%">Customer Number</TD>'
                +'<TD Style="font-weight:bold;" Width="30%">Origination Date</TD>'
                +'</TR>'
                +'</TABLE>';
            document.getElementById('stage3').innerHTML = content;
            for(var i=0; i<loans; i++) {
                content = document.getElementById('stage3').innerHTML;
                document.getElementById('stage3').innerHTML = content
                    + '<TABLE Width="100%">'
                    + '<TR>'
                    + '<TD Width="30%"><INPUT Name="CName'
                    + i
                    + '" Size="40" Type="text"></TD>'
                    + '<TD Width="30%"><INPUT Name="CNumber'
                    + i
                    + '" Size="40" Type="text"></TD>'
                    + '<TD Width="30%"><INPUT Name="Date'
                    + i
                    + '" Size="40" Type="text"></TD>'
                    + '</TR>'
                    + '</TABLE>';
            }
            content = document.getElementById('stage3').innerHTML;
            document.getElementById('stage3').innerHTML = content
                    + '<TABLE><TR><TD><INPUT Type="Button" Value="Submit" onClick="Go3()"></TD></TR></TABLE>';
        }

Now what I need to do is iterate through the form and pull out the values for each of the form fields. This is about as far as I've gotten:

for (var n=0; n<loans; n++) {
content += '<TR>'
    + '<TD Colspan="2">'
    + document.getElementById('CName + n').value
    + '</TD>'
    + '<TD Colspan="2">'
    + document.getElementById('CNumber + n').value
    + '</TD>'
    + '<TD>'
    + document.getElementById('Date + n').value
    + '</TD>'
    + '</TR>';
}

Which does...Nothing. The last notable progress I had was getting it to spit out "null" which isn't really progress at all. I've looked at eval, but there's quite a few warnings against it.

Any Ideas?

3 Answers 3

1

I think you want

document.getElementById('CName' + n).value

(where n is outside of the quotes)

Sign up to request clarification or add additional context in comments.

4 Comments

pastebin.com/eh1Qe7aW is the function as a whole, it still doesn't do anything when I click submit :\
Your inputs also need to be id="CName..." NOT name="CName..."
Nevermind, No i didn't, thank you, I've been staring at this for far to long.
No problem, I know the feeling :)
1

Well it should be 'CName' + n - i.e., you got the quotation marks in the wrong place

Comments

0

If all your controls are in a form, then you can access them as:

var allControls = document.<formId>.elements;

or

var allControls = document.forms[formId].elements;

Then iterate over the list of controls to get the values. The form controls must have names to be successful, no need for IDs. You can also get the values as:

var value = allControls['CName' + n].value;

or just

var form = docment.forms[formID];
var value = form['CName' + n].value;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.