Senseless
Senseless

Reputation: 99

How to populate two dropdown menus with for-loops in a function?

I’m trying to make two drop-down menus containing the same list of currencies by using a for-loop. I have this object:

var ECurrencyTypes = {
    NOK: {value:1.00000, name: "Norske kroner", denomination: "kr"},
    EUR: {value:0.10733, name: "Europeiske euro", denomination: "€"},
    USD: {value:0.12652, name: "United States dollar", denomination: "$"},
    GBP: {value:0.09550, name: "Pound sterling", denomination: "£"},
    INR: {value:8.18624, name: "Indiske rupee", denomination: "र"},
    AUD: {value:0.16129, name: "Australienske dollar", denomination: "A$"},
    PHP: {value:6.48595, name: "Filippinske peso", denomination: "₱"},
    SEK: {value:1.02580, name: "Svenske kroner", denomination: "kr"},
    CAD: {value:0.15841, name: "Canadiske dollar", denomination: "C$"},
    THB: {value:4.18410, name: "Thai baht", denomination: "฿"}
  };

And I have an HTML-page with two forms. The users are supposed to be able to change the preferred currency. In the web-form the fields look like this:

<select name="Valutatype" id="selectCurrencyType"></select>

and

 <select id="selectAccountCurrency"></select>

The function I’m trying to use looks like this:

function loadAllCurrencyKeys() {
  var keys = Object.keys(ECurrencyTypes);
  for (var index = 0; index < keys.length; index++) {
      var currencyKey = keys[index];
      var newOption = document.createElement("option");
      newOption.value = currencyKey;
      newOption.text = ECurrencyTypes[currencyKey].name;
      selectCurrencyType.options.add(newOption);          
  }
}

loadAllCurrencyKeys();

Is it possible to include another for-loop containing selectAccountCurrency into the same function?

Upvotes: 0

Views: 60

Answers (2)

Mario Amandio
Mario Amandio

Reputation: 62

after you create the newOption and config all the properties try this

document.querySelector("#selectAccountCurrency").appendChild(newOption)

Upvotes: 1

Mihai Alexandru-Ionut
Mihai Alexandru-Ionut

Reputation: 48417

The issue is in the below statement

 for (var index = 0; index = keys.lenght; index++) {

Change it to

 for (var index = 0; index < keys.length; index++) {

Upvotes: 1

Related Questions