1

I have a problem with a javascript array: "arrFinal[i] is undefined"

In my script arrFinal is dynamically generated

function fillTextareas () {
var arrFinal = [];
arrFinal[0] = [];

....
....
// Then some code that define the content of arrFinal, the length of arrFinal ( tailleArrFinal, tailleArrSubFinal)
....
....


for(i=0;i<=tailleArrFinal;i++){
        for(j=0;j<tailleArrSubFinal;j++) {
            $("form textarea#t" + i + "_" + j).val(arrFinal[i][j]);
        }
    }
}

When the function is called, a dump show me that the array arrFinal is correctly fill and the script works but i have an alert "arrFinal[i] is undefined". How can i do that without alert ? Thanks !!

1
  • Where is that alert you are calling... ? Commented Jul 4, 2011 at 12:28

2 Answers 2

3

It looks like an off-by-one error in the outer loop.

It should be i < tailleArrFinal, not <=.

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

Comments

1

By looking at your loop I can see at least 2 errors: you miss "var" and ".length" (you have to test for array length!)

try to replace:

for(i=0;i<=tailleArrFinal;i++){
        for(j=0;j<tailleArrSubFinal;j++) {
            $("form textarea#t" + i + "_" + j).val(arrFinal[i][j]);
        }
    }
}

with:

for(var i=0;i<tailleArrFinal.length;i++){
        for(var j=0; j<tailleArrSubFinal.length; j++) {
            $("form textarea#t" + i + "_" + j).val(arrFinal[i][j]);
        }
    }
}

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.