...

View Full Version : "Undefined" array error when populating dropdown box with javascript



pb003
05-10-2009, 11:28 PM
Hello. I use ASP to write the arrays at the start of my page:


<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:


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? :confused:

Thanks in advance for any help.

venegal
05-10-2009, 11:37 PM
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.

adios
05-10-2009, 11:46 PM
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:


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?

pb003
05-11-2009, 12:01 AM
<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:


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).

venegal
05-11-2009, 12:25 AM
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?

pb003
05-11-2009, 12:36 AM
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:


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?

venegal
05-11-2009, 12:52 AM
Alright, I can tell you what's wrong here:

After

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

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

ArrManu1[0] = "Bell";

pb003
05-11-2009, 02:51 PM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum