views:

65

answers:

1

Hello, 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

+1  A: 

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?

Matthew Flaschen
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
@greg, we need a [SSCCE](http://sscce.org/) to be able to help. Post a simple complete page that shows the issue.
Matthew Flaschen
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
@greg, yeah, SSCCE also works as a debugging technique. :)
Matthew Flaschen