Click to See Complete Forum and Search --> : JS arrays


mili
12-12-2003, 06:25 PM
Hi, I created a couple of arrays to store some values.Based on a value selected from ccy select box, I want to populate the corresponding
array into the stgy select box.If I choose another value from ccy select box, I want to be able to remove the
existing value from the stgy select box and populate the second array into it.
This code is not working.Please help me.

Thanks,

<HTML><BODY>

<FORM Name="Test">
<SELECT Name="ccy" onChange="fillstgy();">

<option value="1">TKY</option>
<option value="2">EUR</option>
<option value="3">AUD</option>
<option value="4">USD</option>

</SELECT>
<SELECT Name="stgy" >
<option value="1">select</option>


</SELECT>

</FORM>


<SCRIPT>

var stgyTKY=new Array();
stgyTKY[0]="TKY1";
stgyTKY[1]="TKY2";
stgyTKY[2]="TKY3";

var stgyEUR=new Array();
stgyEUR[0]="EUR1";
stgyEUR[1]="EUR2";
stgyEUR[2]="EUR3";

var frm = document.Test;




// fill a select from contents of an array!
function fill( sel, ar )
{
var ix;
for ( ix = sel.options.length-1; ix >= 0; --ix )
sel.options[ix] = null;
alert("removing elements");
for ( ix = 0; ix < ar.length; ++ix )
sel.options[ix] = new Option( ar[ix][0], ar[ix][1] );
alert("adding elements");
}

function fillstgy()
{
if(document.forms[0].ccy.selectedIndex == 0)
{
alert("JPY");
TKY = stgyTKY;
fill( frm.stgy, TKY );
}
else if(document.forms[0].ccy.selectedIndex == 1)
{
alert("EUR");
EUR = stgyEUR
fill( frm.stgy, EUR );
}
}


</SCRIPT>

</BODY></HTML>

fredmv
12-12-2003, 07:07 PM
Originally posted by mili
This code is not working.Simply saying it's "not working" can mean a lot of different things. What exactly isn't it doing that it should be?

mili
12-12-2003, 07:12 PM
It is wiping out the option values from the existing select box, but not populating the values from array

fredmv
12-12-2003, 07:19 PM
Do you want it to append options to the other select box as opposed to erasing existing values then adding more options? If not, please clarify.

mili
12-14-2003, 10:36 AM
I'm sorry if I did not communicate well.
Here is my problem:

1. The default for ccy list box is "TKY". So I want to load only the options from "stgyTKY" array into stgy list box.
2. When I fire an onchange event on ccy list box, let's say I choose EUR from ccy list box this time, I want to wipe out all the "stgyTKY" options from the stgy list box and load only the options from "stgyEUR" array.

Thanks