so essentially I was given an array of countries and I must first generate a loop into a drop-down list that displays the country names.
Second, the value attributes for each item in the list should be the country code
Then I must add an event handler for the drop-down lists change event so that when a country is selected it will display the capital of the city.
I've tried playing around with loops and change event handlers but I can't seem to figure it out.
Any help is appreciated thank you.
I know it's a mess but I'm having a tough time.
var countries = [{
code: "AR",
name: "Argentina",
capital: "Buenos Aires"
},
{
code: "AT",
name: "Austria",
capital: "Vienna"
},
{
code: "BE",
name: "Belgium",
capital: "Brussels"
},
{
code: "CA",
name: "Canada",
capital: "Ottawa"
}
];
var select = document.getElementById('selectCountries');
for (var i = 0; i < countries.length; i++) {
var optco = countries[i];
var opt = document.createElement('options');
opt.textContent = optco;
opt.value = optco;
select.appendChild(opt);
// loop for grabbing each country name and displaying it in the drop-down
}
document.getElementById('countries').innerHTML
// use an onchange event
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Array Countries</title>
</head>
<body style="background-color: powderblue">
<h1> Country Info Guide </h1>
<p> Please Select a Country for More Information</p>
<form method=get action=whatever.php>
Select a country:
<select id="selectCountries" onchange="GetCapital(this)">
</select>
<div>
<p>
</p>
</div>
</form>
</body>
</html>