Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    "Undefined" array error when populating dropdown box with javascript

    Hello. I use ASP to write the arrays at the start of my page:
    Code:
    <script language="Javascript"> 
    var ArrManu1 = new Array();
    var ArrManu2 = new Array();
    var ArrManu3 = new Array();
    var ArrManu4 = new Array();
    var ManuArr = new Array(3);
    ManuArr = [ArrManu1, ArrManu2, ArrManu3, ArrManu4];
    ArrManu1 = ["Bell"];
    </script>
    ... and my webpage has 2 dropdown boxes. When the user changes the first this function is called:
    Code:
    function handleChange1(Co)
    {   
    var Location, i
    Location = document.fmACMenu.menuManu;
    
    // Delete all entries in the Manufacturer list box
    for (i = Location.length; i > 0; i--)
    {
      Location.options[i-1] = null
    }
    
      // Add manufacturers to the dropdown box
    {
      Location.options[0] = new Option("-- Select Manufacturer --",0);
      i = 1
    do
      {
      Location.options[i] = new Option(((ManuArr[Co-1])[i-1]),(i));
      i++
      }
    while (((ManuArr[Co-1])[i-1]) != null)
    }
    }
    ...to populate the second dropdown box. However, when the user selects the first option in the first dropdown box (therefore making the variable "Co" equal to 1), ((ManuArr[Co-1])[i-1]) is "undefined" - why doesn't it add "Bell" to the dropdown?

    Thanks in advance for any help.

  • #2
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    What are you passing in as "Co"? Show some more code!

    Also, the language property is deprecated, so it's possible that that part of the code is not even interpreted. Use type="text/javascript" instead.

  • #3
    Senior Coder
    Join Date
    Jun 2002
    Posts
    1,404
    Thanks
    2
    Thanked 32 Times in 32 Posts
    Q)
    why doesn't it add "Bell" to the dropdown?
    A) Because "ManuArr[Co-1])[i-1]) is "undefined""

    Why would it work?
    Had trouble following your code. This is silly:

    Code:
    var ArrManu1 = new Array();
    var ArrManu2 = new Array();
    var ArrManu3 = new Array();
    var ArrManu4 = new Array();
    var ManuArr = new Array(3);
    ManuArr = [ArrManu1, ArrManu2, ArrManu3, ArrManu4];
    ArrManu1 = ["Bell"];
    That last just discards the array you put in ArrManu1 and replaces it with another. Could you explain what you need to do here?

  • #4
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Macintosh

    Code:
    <select name="menuType" id="menuType" onchange="handleChange1(this.options[this.selectedIndex].value)" size="1">
    <option value=0>-- Select AC type --</option>                
    <%
      strsql = "select * from tbACType order by ID"
      objrs.open strsql, myconn, 0, 1
      i = 1
      do until objrs.eof
          Response.Write "<option value=" & chr(10) & i & chr(10) & ">" & objrs.fields("Aircraft_Type") & "</option>"
          i = i + 1
          objrs.movenext
      loop
     objrs.Close
    %>
    </select>
    ... is the code that passes "Co" into the function.

    Adios - ignore the first 4 lines of the array code: that was me trying to get it to work. Just look at:
    Code:
    var ManuArr = new Array(3);
    ManuArr = [ArrManu1, ArrManu2, ArrManu3, ArrManu4];
    ArrManu1 = ["Bell"];
    What I am trying to do is populate a second dropdown box, the components of which vary depending upon which option the user chooses in the first (out of 4 possible options)

    Venegal - changing the langugage from Javascript to text/Javascript now gives me an "OBJECT EXPECTED" error when the first dropdown box is changed (see code above).

  • #5
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    You didn't actually change "the langugage from Javascript to text/Javascript", did you?

    Just to make sure, you are supposed to change <script language="Javascript"> to <script type="text/javascript">.

    Is there a reason you have those chr(10) line feeds within your <option> tags?

    Anyway, can you provide a link to the site, so I can see what exactly is causing the object expected error?

  • #6
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Hi - sorry... I think I have been working too long today... forgot to change the "language" to "type" - all sorted now.

    And no... no need for the Chr(10) in it - took them out.

    So back to the original problem - populating the dropdown via arrays. Any idea why the array value is undefined?

    If I run this code in the function:
    Code:
    alert(ManuArr[0][0])
    alert(ArrManu1[0])
    I get "undefined" and then "Bell" - so I know the arrManu1 array is correct, I just can't read the first value in arrMenu1 by going via the multi-dimentional way ManuArr[0][0]. Any idea why not?

  • #7
    Gütkodierer
    Join Date
    Apr 2009
    Posts
    2,127
    Thanks
    1
    Thanked 426 Times in 424 Posts
    Alright, I can tell you what's wrong here:

    After
    Code:
    ManuArr = [ArrManu1, ArrManu2, ArrManu3, ArrManu4];
    ManuArr[0] and ArrManu1 are references to the same array, so changing the contents of either one of them has the same effect.

    But then you go
    Code:
    ArrManu1 = ["Bell"];
    which is effectively changing the variable ArrManu1 to hold a reference to the newly created array ["Bell"], which has now nothing to do with ManuArr any more.

    So, what you want to do is not change the reference the variable is holding, but keeping the reference and change the array that is referenced. You can do so by changing the above line to
    Code:
    ArrManu1[0] = "Bell";

  • Users who have thanked venegal for this post:

    pb003 (05-11-2009)

  • #8
    New to the CF scene
    Join Date
    May 2009
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts
    A-ha... thank you. This does indeed work now (no surprise to you I am sure).

    Rather than asping the array in long format (eg: ArrManu1[0] = "Bell"; ArrManu1[1] = "Whistle") am I able to shortening the code and populate in one line, similar to: ArrManu1 = ["Bell", "Whistle"] etc

    STOPPRESS: I just tried putting the line ManuArr = [ArrManu1, ArrManu2, ArrManu3, ArrManu4]; AFTER the ArrManu1 = ["Bell", "Whistle"]; it works - I think I will do it that way round. Thanks for your help.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •