0

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

2
  • 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". Commented Aug 19, 2010 at 1:34
  • d'oh.. fixed, but didnt help my issue. Commented Aug 19, 2010 at 1:48

2 Answers 2

1

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?

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

3 Comments

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, we need a SSCCE to be able to help. Post a simple complete page that shows the issue.
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
1

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

var mycount = document.getElementById('counter').value;
mycount = parseInt(mycount);

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.