Click to See Complete Forum and Search --> : Why doesn`t this work? PLZ help.


Taosan
10-06-2003, 05:35 AM
I want to have more population dropdown boxes (see below script) but it gives me an error on the line "for (var q=myChoices.thirdChoice.options.length;q>=0;q--) myChoices.thirdChoice.options[q] = null; " Who can help me out!? I want in totally 4 dropdown boxes....

Thank you in advanced!




SCRIPT LANGUAGE="JavaScript">

<!-- Begin
var arrItems1 = new Array();
var arrItemsGrp1 = new Array();

arrItems1[3] = "Truck";
arrItemsGrp1[3] = 1;
arrItems1[4] = "Train";
arrItemsGrp1[4] = 1;
arrItems1[5] = "Car";
arrItemsGrp1[5] = 1;

arrItems1[6] = "Boat";
arrItemsGrp1[6] = 2;
arrItems1[7] = "Submarine";
arrItemsGrp1[7] = 2;

arrItems1[0] = "Planes";
arrItemsGrp1[0] = 3;
arrItems1[1] = "Ultralight";
arrItemsGrp1[1] = 3;
arrItems1[2] = "Glider";
arrItemsGrp1[2] = 3;

var arrItems2 = new Array();
var arrItemsGrp2 = new Array();

arrItems2[21] = "747";
arrItemsGrp2[21] = 0
arrItems2[22] = "Cessna";
arrItemsGrp2[22] = 0

arrItems2[31] = "Kolb Flyer";
arrItemsGrp2[31] = 1
arrItems2[34] = "Kitfox";
arrItemsGrp2[34] = 1

arrItems2[35] = "Schwietzer Glider";
arrItemsGrp2[35] = 2

arrItems2[99] = "Chevy Malibu";
arrItemsGrp2[99] = 5
arrItems2[100] = "Lincoln LS";
arrItemsGrp2[100] = 5
arrItems2[57] = "BMW Z3";
arrItemsGrp2[57] = 5

arrItems2[101] = "F-150";
arrItemsGrp2[101] = 3
arrItems2[102] = "Tahoe";
arrItemsGrp2[102] = 3

arrItems2[103] = "Freight Train";
arrItemsGrp2[103] = 4
arrItems2[104] = "Passenger Train";
arrItemsGrp2[104] = 4

arrItems2[105] = "Oil Tanker";
arrItemsGrp2[105] = 6
arrItems2[106] = "Fishing Boat";
arrItemsGrp2[106] = 6

arrItems2[200] = "Los Angelas Class";
arrItemsGrp2[200] = 7
arrItems2[201] = "Kilo Class";
arrItemsGrp2[201] = 7
arrItems2[203] = "Seawolf Class";
arrItemsGrp2[203] = 7

var arrItems3 = new Array();
var arrItemsGrp3 = new Array();

arrItems3[3] = "Truck";
arrItemsGrp3[3] = 1;
arrItems3[4] = "Train";
arrItemsGrp3[4] = 1;
arrItems3[5] = "Car";
arrItemsGrp3[5] = 1;

arrItems3[6] = "Boat";
arrItemsGrp3[6] = 2;
arrItems3[7] = "Submarine";
arrItemsGrp3[7] = 2;

arrItems3[0] = "Planes";
arrItemsGrp3[0] = 3;
arrItems3[1] = "Ultralight";
arrItemsGrp3[1] = 3;
arrItems3[2] = "Glider";
arrItemsGrp3[2] = 3;

var arrItems4 = new Array();
var arrItemsGrp4 = new Array();

arrItems4[21] = "747";
arrItemsGrp4[21] = 0
arrItems4[22] = "Cessna";
arrItemsGrp4[22] = 0

arrItems4[31] = "Kolb Flyer";
arrItemsGrp4[31] = 1
arrItems4[34] = "Kitfox";
arrItemsGrp4[34] = 1

arrItems4[35] = "Schwietzer Glider";
arrItemsGrp4[35] = 2

arrItems4[99] = "Chevy Malibu";
arrItemsGrp4[99] = 5
arrItems4[100] = "Lincoln LS";
arrItemsGrp4[100] = 5
arrItems4[57] = "BMW Z3";
arrItemsGrp4[57] = 5

arrItems4[101] = "F-150";
arrItemsGrp4[101] = 3
arrItems4[102] = "Tahoe";
arrItemsGrp4[102] = 3

arrItems4[103] = "Freight Train";
arrItemsGrp4[103] = 4
arrItems4[104] = "Passenger Train";
arrItemsGrp4[104] = 4

arrItems4[105] = "Oil Tanker";
arrItemsGrp4[105] = 6
arrItems4[106] = "Fishing Boat";
arrItemsGrp4[106] = 6

arrItems4[200] = "Los Angelas Class";
arrItemsGrp4[200] = 7
arrItems4[201] = "Kilo Class";
arrItemsGrp4[201] = 7
arrItems4[203] = "Seawolf Class";
arrItemsGrp4[203] = 7


function selectChange(control, controlToPopulate, ItemArray, GroupArray)
{
var myEle ;
var x ;
// Empty the second drop down box of any choices
for (var q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;
if (control.name == "firstChoice") {
// Empty the third drop down box of any choices
for (var q=myChoices.thirdChoice.options.length;q>=0;q--) myChoices.thirdChoice.options[q] = null;
}
// ADD Default Choice - in case there are no values
myEle = document.createElement("option") ;
myEle.value = 0 ;
myEle.text = "[SELECT]" ;
controlToPopulate.add(myEle) ;
// Now loop through the array of individual items
// Any containing the same child id are added to
// the second dropdown box
for ( x = 0 ; x < ItemArray.length ; x++ )
{
if ( GroupArray[x] == control.value )
{
myEle = document.createElement("option") ;
myEle.value = x ;
myEle.text = ItemArray[x] ;
controlToPopulate.add(myEle) ;
}
}
}
// End -->
</script>

</HEAD>

gil davis
10-06-2003, 03:01 PM
An OPTIONS array is enumerated as 0 to (length - 1). The myChoices.thirdChoice.options[q] when q = myChoices.thirdChoice.options.length does not exist. Therefore, you get an error.

Taosan
10-07-2003, 06:53 AM
Then how should I do it?? I am a newbie to javascipts. Please me help out.....

gil davis
10-07-2003, 01:10 PM
for (var q=controlToPopulate.options.length;q>=0;q--)should be
for (var q=controlToPopulate.options.length-1;q>=0;q--)

Taosan
10-08-2003, 02:32 PM
I have changed the line according to your advise but it didn`t help. I got the error message:

Warning 5002 variable "q" already defined
The variable name was already used inside this function.

gil davis
10-08-2003, 05:09 PM
function selectChange(control, controlToPopulate, ItemArray, GroupArray)
{
var myEle;
var x;
var q;
// Empty the second drop down box of any choices
for (q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;
if (control.name == "firstChoice") {
// Empty the third drop down box of any choices
for (q=myChoices.thirdChoice.options.length;q>=0;q--) myChoices.thirdChoice.options[q] = null;
}
// ADD Default Choice - in case there are no values
myEle = document.createElement("option") ;
myEle.value = 0 ;
myEle.text = "[SELECT]" ;
controlToPopulate.add(myEle) ;
// Now loop through the array of individual items
// Any containing the same child id are added to
// the second dropdown box
for ( x = 0 ; x < ItemArray.length ; x++ )
{
if ( GroupArray[x] == control.value )
{
myEle = document.createElement("option") ;
myEle.value = x ;
myEle.text = ItemArray[x] ;
controlToPopulate.add(myEle) ;
}
}
}

Taosan
10-09-2003, 01:00 AM
when I was reading your notes I suddenly saw the previous script of my that wasn`t complete displaying. I must do something wrong with copy/cute keys. I am so sorry for the inconvenience that I made. The complete scripts is : (by the way, the last changes didn`t help either).


<SCRIPT LANGUAGE="JavaScript">

<!-- Begin

var arrItems1 = new Array();
var arrItemsGrp1 = new Array();

arrItems1[3] = "Truck";
arrItemsGrp1[3] = 1;
arrItems1[4] = "Train";
arrItemsGrp1[4] = 1;
arrItems1[5] = "Car";
arrItemsGrp1[5] = 1;

arrItems1[6] = "Boat";
arrItemsGrp1[6] = 2;
arrItems1[7] = "Submarine";
arrItemsGrp1[7] = 2;

arrItems1[0] = "Planes";
arrItemsGrp1[0] = 3;
arrItems1[1] = "Ultralight";
arrItemsGrp1[1] = 3;
arrItems1[2] = "Glider";
arrItemsGrp1[2] = 3;


var arrItems2 = new Array();
var arrItemsGrp2 = new Array();

arrItems2[21] = "747";
arrItemsGrp2[21] = 0
arrItems2[22] = "Cessna";
arrItemsGrp2[22] = 0

arrItems2[31] = "Kolb Flyer";
arrItemsGrp2[31] = 1
arrItems2[34] = "Kitfox";
arrItemsGrp2[34] = 1

arrItems2[35] = "Schwietzer Glider";
arrItemsGrp2[35] = 2

arrItems2[99] = "Chevy Malibu";
arrItemsGrp2[99] = 5
arrItems2[100] = "Lincoln LS";
arrItemsGrp2[100] = 5
arrItems2[57] = "BMW Z3";
arrItemsGrp2[57] = 5

arrItems2[101] = "F-150";
arrItemsGrp2[101] = 3
arrItems2[102] = "Tahoe";
arrItemsGrp2[102] = 3

arrItems2[103] = "Freight Train";
arrItemsGrp2[103] = 4
arrItems2[104] = "Passenger Train";
arrItemsGrp2[104] = 4

arrItems2[105] = "Oil Tanker";
arrItemsGrp2[105] = 6
arrItems2[106] = "Fishing Boat";
arrItemsGrp2[106] = 6

arrItems2[200] = "Los Angelas Class";
arrItemsGrp2[200] = 7
arrItems2[201] = "Kilo Class";
arrItemsGrp2[201] = 7
arrItems2[203] = "Seawolf Class";
arrItemsGrp2[203] = 7


var arrItems3 = new Array();
var arrItemsGrp3 = new Array();

arrItems3[3] = "Truck";
arrItemsGrp3[3] = 1;
arrItems3[4] = "Train";
arrItemsGrp3[4] = 1;
arrItems3[5] = "Car";
arrItemsGrp3[5] = 1;

arrItems3[6] = "Boat";
arrItemsGrp3[6] = 2;
arrItems3[7] = "Submarine";
arrItemsGrp3[7] = 2;

arrItems3[0] = "Planes";
arrItemsGrp3[0] = 3;
arrItems3[1] = "Ultralight";
arrItemsGrp3[1] = 3;
arrItems3[2] = "Glider";
arrItemsGrp3[2] = 3;


var arrItems4 = new Array();
var arrItemsGrp4 = new Array();

arrItems4[21] = "747";
arrItemsGrp4[21] = 0
arrItems4[22] = "Cessna";
arrItemsGrp4[22] = 0

arrItems4[31] = "Kolb Flyer";
arrItemsGrp4[31] = 1
arrItems4[34] = "Kitfox";
arrItemsGrp4[34] = 1

arrItems4[35] = "Schwietzer Glider";
arrItemsGrp4[35] = 2

arrItems4[99] = "Chevy Malibu";
arrItemsGrp4[99] = 5
arrItems4[100] = "Lincoln LS";
arrItemsGrp4[100] = 5
arrItems4[57] = "BMW Z3";
arrItemsGrp4[57] = 5

arrItems4[101] = "F-150";
arrItemsGrp4[101] = 3
arrItems4[102] = "Tahoe";
arrItemsGrp4[102] = 3

arrItems4[103] = "Freight Train";
arrItemsGrp4[103] = 4
arrItems4[104] = "Passenger Train";
arrItemsGrp4[104] = 4

arrItems4[105] = "Oil Tanker";
arrItemsGrp4[105] = 6
arrItems4[106] = "Fishing Boat";
arrItemsGrp4[106] = 6

arrItems4[200] = "Los Angelas Class";
arrItemsGrp4[200] = 7
arrItems4[201] = "Kilo Class";
arrItemsGrp4[201] = 7
arrItems4[203] = "Seawolf Class";
arrItemsGrp4[203] = 7


function selectChange(control, controlToPopulate, ItemArray, GroupArray)
{
var myEle ;
var x ;
var q ;
// Empty the second drop down box of any choices
for (q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;
if (control.name == "firstChoice") {
// Empty the third drop down box of any choices
for (q=myChoices.thirdChoice.options.length;q>=0;q--) myChoices.thirdChoice.options[q] = null;
}
// ADD Default Choice - in case there are no values
myEle = document.createElement("option") ;
myEle.value = 0 ;
myEle.text = "[SELECT]" ;
controlToPopulate.add(myEle) ;
// Now loop through the array of individual items
// Any containing the same child id are added to
// the second dropdown box
for ( x = 0 ; x < ItemArray.length ; x++ )
{
if ( GroupArray[x] == control.value )
{
myEle = document.createElement("option") ;
myEle.value = x ;
myEle.text = ItemArray[x] ;
controlToPopulate.add(myEle) ;
}
}
}
// End -->
</script>

</HEAD>

<BODY>

<form name=myChoices>
<table align="center">
<tr>
<td>
<SELECT id=firstChoice name=firstChoice onchange="selectChange(this, myChoices.secondChoice, arrItems1, arrItemsGrp1);">
<option value=0 SELECTED>[SELECT]</option>
<option value=1>Land</option>
<option value=2>Sea</option>
<option value=3>Air</option>
</SELECT>
</TD><TD>
<SELECT id=secondChoice name=secondChoice onchange="selectChange(this, myChoices.thirdChoice, arrItems2, arrItemsGrp2);">
</SELECT>
<SELECT id=thirdChoice name=thirdChoice>
</SELECT>
</TD>
<td>
<SELECT id=firstChoice name=firstChoice onchange="selectChange(this, myChoices.secondChoice, arrItems3, arrItemsGrp3);">
<option value=0 SELECTED>[SELECT]</option>
<option value=1>Land</option>
<option value=2>Sea</option>
<option value=3>Air</option>
</SELECT><TD>
<SELECT id=secondChoice name=secondChoice onchange="selectChange(this, myChoices.thirdChoice, arrItems4, arrItemsGrp4);">
</SELECT>
<SELECT id=thirdChoice name=thirdChoice>
</SELECT>
</TD>
<td>
</TD>

</TR>
</TABLE>
</form>


<p><center>