I have a JSON file. It looks like this:
{
"AL": "Alabama",
"AK": "Alaska",
"AS": "American Samoa",
"AZ": "Arizona",
"AR": "Arkansas",
"CA": "California"
}
How do I loop through every entry in this JSON file and add it to this dropdown?
<select name="states" id="states"> <option value="Alabama">Volvo</option> </select>
please try the below code.
<button onclick="add()">Add</button>
<select id="a"></select>
<div id="demo"></div>
<script>
function add(){
var json = JSON.parse('{"AL": "Alabama","AK": "Alaska","AS": "American Samoa","AZ": "Arizona","AR": "Arkansas","CA": "California"}');
console.log(json);
for( x in json)
{
document.querySelector('#a').innerHTML += "<option value='" + json[x] + "'>" + json[x] + "</option>"
}
}
</script>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.