0

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.

gdoron is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
DEVOPS
  • 17,240
  • 30
  • 93
  • 117

2 Answers2

8
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. 
gdoron is supporting Monica
  • 142,542
  • 55
  • 282
  • 355
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;      
}
Muhammad Raheel
  • 19,645
  • 7
  • 66
  • 101