1

I have global array variable so i can access it from many functions. Problem occurs when im trying to use this variable in function that is bind to button created dynamically, variable is undefined then.

var array = [[]];

  $('#edit').on("click", "input[id='button']", function(){

... i can't use this variable here and i wanna to put some values:- array[index1][index2]=...

});

I even made function to get this array and assign to new variable, and i can get values that was already in this array, but i can't add new ones cause of error "can't convert undefined to object". What seems to be the problem, and how to solve that ? Maybe some workaround ?

4
  • "input[id='button']" = #button Commented Mar 28, 2014 at 14:55
  • I don't have problem with selector :> Commented Mar 28, 2014 at 15:01
  • I know - it was merely a suggestion. Commented Mar 28, 2014 at 15:02
  • Thx,i know this, but i use a lot with "^=" so i went for longer version ;d Commented Mar 28, 2014 at 15:06

1 Answer 1

1

Are you sure your indexes are correct? On the initialization you are creating an empty array at array[0], so the first added value would be at array[1]. The inner brackets are not necessary for a multidimensional array, it's just var array = []; unless you wanted to initialize an empty array at index 0. Also when you're assigning the value, make sure the second dimension of the array is initialized:

var array = [[]];    // creates an empty array at index 0, so we'll add at 1
array[1][0] = 'foo'; // won't work since array is not initialized
array[1] = []; 
array[1][0] = 'bar'; // works

If that doesn't help try posting more code, like how your indexes are generated.

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

1 Comment

Yeah that helped. I still can't reach that global variable, but my workaround now works.You were right, problem was with initializing second dimension for higher index of array. Thanks.

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.