Skip to content

Instantly share code, notes, and snippets.

@bpolaszek
Created July 17, 2020 09:13
Show Gist options
  • Save bpolaszek/5dbb058b6b427357d115056d7d9511aa to your computer and use it in GitHub Desktop.
Save bpolaszek/5dbb058b6b427357d115056d7d9511aa to your computer and use it in GitHub Desktop.
Vanilla JS snippet to generate a SELECT country list from an API.
<select id="country" name="country">
<option/>
</select>
<script>
(async () => {
const response = await fetch('https://restcountries.eu/rest/v2/all?fields=alpha2Code;name');
const countries = await response.json();
const select = document.getElementById('country');
for (const country of countries) {
const option = document.createElement('option');
option.value = country.alpha2Code;
option.innerText = country.name;
select.appendChild(option);
}
})();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment