2

I need to create global two dimensional array in jquery or javascript

My function is like this

<script>

var globalArray[0] = new Array();

function createArray(){

    alert(globalArray[0]);         
}

</script>

<div><input type='button' value='save' onclick='createArray();'> </div>

On click of that button I am getting this error "globalArray[0] is undefined"

How can I create global dynamic multi dimensional array.

4
  • Is that dot $.("#uname") a typo? Commented Jun 4, 2012 at 10:42
  • What are you trying to do with this code? Commented Jun 4, 2012 at 10:46
  • 1
    @gdoron Seems like simple element pushing but in a bit strange way. Commented Jun 4, 2012 at 10:48
  • index never changes, so why is it even there? also uname is undefined. Lack of details is causing more questions than answers Commented Jun 4, 2012 at 10:48

2 Answers 2

9
if (!globalArray[index]) 
    globalArray[index] = []; // init the array.

globalArray[index].push(name);

You have a typo with the dot:

$.("#uname").val(); 

Change to:

$("#uname").val();

What are you trying to do with this code?


Update: (The question was totally edited.)

Your code:

var globalArray[0] = new Array(); 

globalArray[0] is invalid variable name, you need first to declare the array:

var globalArray = []; // Array literal.
globalArray[0] =  [] // The element at position 0 is new an array. 
Sign up to request clarification or add additional context in comments.

2 Comments

Just globalArray[index].push(uname).
I updated my question. What ever you posted is not resolve my issue
1

Intead of

if(loop == 0){
 globalArray[index][0] = uname;
}else{
  globalArray[index][loop++] = uname;
}

Use this

if(loop > 0){
    globalArray[index][loop++] = uname;     
}else{
    globalArray[index][0] = uname;      
}

4 Comments

i haven't even done anything yet
Then I'm sorry, but what do you mean by "yet"?
no activity i mean wont do it either not greedy for votes ups and downs
I updated my question. please check it

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.