0

I am trying to read values from a form dynamically created by user and insert them in an 2 dimensional array. The ID of any field is created dynamically and it has the value 11, 12, 13 etc, depending on the rows and cols of the table. I try to read it using getElementByID but it seems to not work. The function is as follows:

            function readMatrix () {
        for (i = 0; i < k; i++) {
            for (j = 0; j < k; j++)
            A[i][j]=parseFloat(getElementById(ij).value, 10);
        }
    }
3
  • 3
    You are leaking globals i and j. Use var to avoid this!! And parseFloat does not have a 2nd base argument. Unless you are using HTML5 you also cannot have an ID starting with a number. Commented May 13, 2012 at 19:14
  • my bad to the below comments. it works perfectly, i made a mistake some place else in the script Commented May 13, 2012 at 19:41
  • You firebug (if you are not using it) Highly recommended. getfirebug.com/ Commented May 13, 2012 at 19:53

3 Answers 3

2

First, it's document.getElementById(...) - that part is important, otherwise it can't find the getElementById function.

Next, you're having it look for a variable called ij, which is not what you want. You want the two numbers concatenated, so that can either be i*10+j or ""+i+j, whichever you prefer.

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

2 Comments

It does work, you're just doing something else wrong. Please show your updated code.
yes you are right I was doing something else wrong. Thank you
1

I guess, elements id is concantenation of i and j , rather than addition.

So, it should work,

   function readMatrix () {
        for (i = 0; i < k; i++) {
            for (j = 0; j < k; j++)
            A[i][j]=parseFloat(document.getElementById(i + '' + j).value, 10);
        }
    }

2 Comments

You are trying to get value of which html element ?
Mark question as answer that helps you :)
1

You have a problem with getElementById call and argument. Try this:

function readMatrix() {
    for (var i = 0; i < k; i++) {
        for (var j = 0; j < k; j++) {
            var id = "" + (i + 1) + (j + 1);
            A[i][j] = parseFloat(document.getElementById(id).value);
        }
    }
}​

Pay attention you have IDs starting from 11 but not 00, so (i + 1) + (j + 1) part is added.

DEMO: http://jsfiddle.net/VnvM2/

1 Comment

the +1 part seems legit, however the function is not working in this form, nor with "A"+(i+1)+(j+1)

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.