Created
July 17, 2020 09:13
-
-
Save bpolaszek/5dbb058b6b427357d115056d7d9511aa to your computer and use it in GitHub Desktop.
Vanilla JS snippet to generate a SELECT country list from an API.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<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