[thelist] Multidimensional Arrays in JavaScript

Joshua Olson joshua at waetech.com
Tue Sep 24 22:41:03 CDT 2002


----- Original Message -----
From: "Ken Kogler" <ken.kogler at curf.edu>
Sent: Tuesday, September 24, 2002 11:18 PM


> As you can see, I'm going through the recordset row by row and trying to
> create a multidimensional array. Later on in the code (fired by an
> onClick), I have this:
>
>   alert(aPrice[10][2]);
>
> Which should return the price of a certain sign, but doesn't seem to
> work. I get an "aPrice is undefined" error.

Move the var aPrice to the top level of the script block.  By defining it
within the function, you are making it local.  As soon as the function is
ended, the variable become vapor.  So, you should have something like:

  <script type="text/javascript">    var aPrice;
    function pRec(size, material, price) {
      this.size = size
      this.material = material
      this.price = price
    }
    function makeItSo() {
      aPrice = new Array(24);
      aPrice[0] = new pRec("10 x 12",".024 Alumnium","9.35");      ...
      aPrice[24] = new pRec("7 x 10",".060 Polystyrene","4.4");
    }
    window.onload = makeItSo();
  </script>
HTH,
-joshua




More information about the thelist mailing list