Click here to Skip to main content
15,888,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to make a form in HTML that asks your name, last name and country.
Names are no problems, but countries are. I want (need) to use a dropdown list with all the countries of the world, the options of the dropdown list are all elements from a javascript array.

The way I made the js file is like this: i made a function init(){
let list_countries=['afghanistan',...] so I made a var list_functions with all the countries, after that I ran the function
init().

The thing is, how can I represent each of these countries as an option in my dropdown list? I'd like to do this part of coding in html tags

What I have tried:

I tried things like:
HTML
<select id="country_list"></select>
    <script src="script.js">
    </script> 
</select><br></br>

I tried swapping select and script etc, but I have no clue :/

I tried googling/finding the solution online, but I can't find a perfect solution to my question.
Posted
Updated 17-Mar-18 14:00pm

1 solution

Here's the idea:
HTML
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
<script type="text/javascript">
  function countries_ddl() {
     var countries = ["England", "France", "Germany"];
     var select = document.getElementById('country_list');
     for (var i = 0; i < countries.length; i++) {
        var opt = document.createElement('option');
        opt.value = i;
        opt.innerHTML = countries[i];
        select.appendChild(opt);
     }
}
</script>
</head>
<body onload="countries_ddl()">
		<select id="country_list">
         <option value="0">- please select -</option>
		</select>		
</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900