I have the following data in a json file. I want this data to be shown in dropdown lists, as: In first drop down, I can select the country. In second drop down, I can select from the states of the selected country. Then, the population of the place is shown in text below on the html page.
[
{
"country": "USA",
"state": "Texas",
"population": "20M"
},
{
"country": "USA",
"state": "Alabama",
"population": "4M"
},
{
"country": "USA",
"state": "California",
"population": "33M"
},
{
"country": "India",
"state": "Maharashtra",
"population": "112M"
},
{
"country": "Japan",
"state": "Tokyo",
"population": "13M"
},
{
"country": "India",
"state": "Bihar",
"population": "104M"
},
{
"country": "USA",
"state": "Florida",
"population": "15M"
},
{
"country": "India",
"state": "Gujarat",
"population": "60M"
},
{
"country": "USA",
"state": "Georgia",
"population": "8M"
},
{
"country": "Japan",
"state": "Osaka",
"population": "8M"
},
{
"country": "Japan",
"state": "Saitama",
"population": "7M"
},
{
"country": "India",
"state": "Tamil Nadu",
"population": "72M"
}
]
It will be more helpful if the code is in javascript. Thank you:)
I have tried this:--
<body>
<p>
<input type="button" style="margin:10px 0; "onclick = "populateSelect()" value = "Start" />
</p>
<select id="sel" onchange="show(this)">
<option value="">-- Select --</option>
</select>
</body>
<script>
var ele = document.getElementById('sel');
for (var i = 0; i < data.length; i++) {
if(data[i]['country']!==undefined){
ele.innerHTML = ele.innerHTML + '<option value="' + data[i]['country'] + '">' + data[i]['country'] + '</option>'; }
</script>