Javascript: Why doesn't my array of arrays work?

Bes

Bes

Soldato
Joined
18 Oct 2002
Posts
7,318
Location
Melbourne
Hi,

I am trying to create an array of arrays in Javascript to hold data on a set of data from a form of an unknown size. This is what I have so far:
PHP:
   <script>
  $(document).ready(function(){
	var formTypes=new Array("GlobCol","CoOrd");
	var fTWrapper=new Array();
	for ( var i=0, len=formTypes.length; i<len; ++i ){
  	var input= $("input[name^=" + formTypes[i] + "]");
  	var multipleValues = $("input[name^=" + formTypes[i] + "]").val();
		var values=new Array();
		for (x=1; x<=input.length; x++)
		{
   		values[x]=$("input[name^=" + formTypes[i] + x + "]").val();
		}    
	
	 fTWrapper[i]=values;
		$("div").text("For the field type"+ formTypes[i] +" jQuery found.."+ input.length +"."+fTWrapper[1]+" and " +x+ " elements in the array. Last Testing.." + values[5]);
		
	}
  });
  </script>

This works perfectly, except if I replace ftWrapper[1] in the last line with ftWrapper[1][1], I get no output and a script error (At the moment I get the contents of Values seperated with commas)... I have no idea why.

Can anyone tell me where I am going wrong please?

Thanks
 
Last edited:
$ is a jquery function in this instance.

I think he means that there are times when it's used for no reason eg:

Code:
var input= $("input[name^=" + formTypes[i] + "]");

could just as easily be:
Code:
var input= "input[name^=" + formTypes[i] + "]";

Using jQuery on a string is for creating DOM elements or using CSS to select an existing DOM element... just putting in a regular string doesn't really do anything, and in theory I'd have built jQuery to fail if the syntax didn't validate as html or css, but I think it probably just silently does nothing and leaves it as a string.

Javascript doesn't support two dimension arrays. I have implemented a solution before but I can't seem to find it atm.

I'll do some digging on my work pc tomorrow.

Yeah, the way I was taught to do it is to do arrays full of arrays, full of arrays, etc etc.
 
Last edited:
Hey guys, sorry been away for a few days... thanks for the hints I will look at this again tonight :)
 
The funny thing is that is it printing out all of the elements of the values array as ftwrapper[1] or ftwrapper[0], but it simply is not picking out an individual element.
 
Back
Top Bottom