Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an serverside script making an amount of text fields for me. When I want a user to fill them up, and submit data. I know how many fields there are, as the server also sends a count.

I am then trying to join them into a long string with a spacer between. But I am having trouble getting the value of the array.

Better explained with code.

This works

        <script>
function Submit() {
    var spacer = ":";
    var mycount = document.getElementById('counter').value;
    var usertext = '';
    var x=0;
    for(x = 0; x **<= 2**; x++){
        usertext = usertext + document.getElementById('description[' + x + ']').value + spacer ;
    }
</script>

This does not work.

    <script>
function Submit() {
    var spacer = ":";
    var mycount = document.getElementById('counter').value;
    var usertext = '';
    var x=0;
    for(x = 0; x **<= mycount**; x++){
        usertext = usertext + document.getElementById('description[' + x + ']').value + spacer ;
    }
</script>

This is my body

    <textarea   id='counter' name='counter'>2</textarea>
<textarea   id='description[0]' name=''description'>zero</textarea>
<textarea   id='description[1]' name=''description'>one</textarea>
<textarea   id='description[2]' name=''description'>two</textarea>
<button type="button" onclick="Submit()" >Save</button>

This is the error Firebug gives me:

document.getElementById("description[" + x + "]") is null

Does anyone know a way to do this?

thanks

share|improve this question
Note sure if it is relevant to the problem, but you have a problem with the quotes in here: name=''description'. Note the double single quote before "description". – Daniel Vassallo Aug 19 '10 at 1:34
d'oh.. fixed, but didnt help my issue. – greg Aug 19 '10 at 1:48

2 Answers

Before applying mycount to a for loop take the integer value of mycount with parseInt:

var mycount = document.getElementById('counter').value;
mycount = parseInt(mycount);
share|improve this answer

It works fine for me despite your invalid HTML (e.g. unbalanced quotes). The better question is what the purpose of this is. Why not just submit the form as is?

share|improve this answer
that JS Fiddle is a cool tool, didnt know it existed. It works for me also in that tool, but copy that same code locally, and I cant get it to work in IE, FF or Chrome. Why, because I actually have to output it for a legacy app. – greg Aug 19 '10 at 1:54
@greg, we need a SSCCE to be able to help. Post a simple complete page that shows the issue. – Matthew Flaschen Aug 19 '10 at 2:07
wow, so in the process of making a SSCCE, I somehow fixed it. But no idea how. If I work it out, I will post. Weird. thanks guy – greg Aug 19 '10 at 2:23
@greg, yeah, SSCCE also works as a debugging technique. :) – Matthew Flaschen Aug 19 '10 at 2:29

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.