Forum Moderators: open

Message Too Old, No Replies

Array order

sequential order of array

         

o trenseta o

4:00 am on Jun 7, 2004 (gmt 0)

10+ Year Member



Hi im trying to get my array to list items in a select field the same way they are in the array.

e.g
var country = {
'73¢' : 'Afghanistan',
'28¢' : 'Albania',
'36¢' : 'Albania Mobile',
'27¢' : 'Algeria',
'9¢' : 'Andorra',
}

<form action="" name="rates">
<select class="country" onchange="thiswillswapthevalues();" name="main">
<option selected="selected">-Select from the list</option>
<script type="text/javascript">
for( var opt in country ){document.write( '<option value="' + opt + '">' + country [opt ] + '</option>' );}
</script>
</select>
</form>

But when it's outputted ,the select box( where the values are being placed into) are all over the place.

i was wondering if there is a way/line of code that would make the items retain their position in the array = country once in the select box?

Kind regards

o_trenseta_o

Purple Martin

4:21 am on Jun 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It doesn't look like that syntax will work. I suggest you do it the simple way, with two parallel arrays, with their values declared the long-winded way (to keep it all easy to understand):

<script type="text/javascript">

var money = new Array();
var country = new Array();

money[0] = '73&cent;';
country[0] = 'Afghanistan';

money[1] = '28&cent;';
country[1] = 'Albania';

money[2] = '36&cent;';
country[2] = 'Albania Mobile';

money[3] = '27&cent;';
country[3] = 'Algeria';

money[4] = '9&cent;';
country[4] = 'Andorra';

</script>

<form action="" name="rates">
<select class="country" onchange="thiswillswapthevalues();" name="main">
<option selected="selected">-Select from the list</option>
<script type="text/javascript">
for( var i = 0; i < country.length; i++ ) { document.write( '<option value="' + money [i] + '">' + country [i] + '</option>' ) ; }
</script>
</select>
</form>

o trenseta o

4:38 am on Jun 7, 2004 (gmt 0)

10+ Year Member



thanks for the response, the selections are in order now,
and i thank you for you're help.

o_trenseta_o