0

I'm having trouble reading JSON files for inclusion in the drop-down list.

JSON:

{
"BD": "Bangladesh",
"BE": "Belgium",
"BF": "Burkina Faso",
"BG": "Bulgaria",
"BA": "Bosnia and Herzegovina"
}

I want to put values into the dropdownlist as follows: Example:

<select name="optCountry" id="opt_country">
   <option value="BD">Bangladesh</option>
   <option value="BE">Belgium</option>
   <option value="BF">Burkina Faso</option>
   <option value="BG">Bulgaria</option>
   <option value="BH">Bosnia and Herzegovina</option>
</select>

5 Answers 5

2

Try with JSON.parse() use with convert the json string to Object and Object.keys() use with separate the key value to array .Then iterate the key value using Array#forEach .document.createElement used for create the select element form the Dom

var a = '{"BD": "Bangladesh","BE": "Belgium","BF": "Burkina Faso","BG":"Bulgaria","BA": "Bosnia and Herzegovina"}'
var obj = JSON.parse(a);
var select = document.createElement('SELECT')
select.name="optCountry" 
select.id="opt_country"
document.body.appendChild(select)

Object.keys(obj).forEach(function(a){
document.getElementById('opt_country').innerHTML +='<option value="'+a+'">'+obj[a]+'</option>'
})

Sign up to request clarification or add additional context in comments.

Comments

0

How about something like this? It is considered that your JSON data is stored in the variable js_data:

const droplist = document.getElementById("opt_country")
droplist.innerHTML = "" //clear the dropdown list
Object.getOwnPropertyNames(js_data).forEach(function(element){
    droplist.innerHTML += '<option value="'+element+'">'+js_data[element]+'</option>'
})

Comments

0

Iterate over your json by Object.prototype.hasOwnProperty() and make an option group.then append it to a select element using innerHTML.

var json = '{"BD": "Bangladesh","BE": "Belgium","BF": "Burkina Faso","BG":"Bulgaria","BA": "Bosnia and Herzegovina"}';
var data = JSON.parse(json);
var result='';
for (var property in data) {
    if (data.hasOwnProperty(property)) {
	result+='<option value="'+property+'">'+data[property]+'</option>';
    }
}
document.getElementById('opt_country').innerHTML=result;
<select name="optCountry" id="opt_country">
</select>

Comments

0

Try this code

var Country= {
"BD": "Bangladesh",
"BE": "Belgium",
"BF": "Burkina Faso",
"BG": "Bulgaria",
"BA": "Bosnia and Herzegovina"
}
var strInnerHTML = "";
$.each(Country, function( index, value ) {
  strInnerHTML +='<option value="'+index+'">'+value+'</option>';
});
 document.getElementById('opt_country').innerHTML = strInnerHTML;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<select name="optCountry" id="opt_country">
</select>

Comments

0
    var obj = JSON.parse('{
    "BD": "Bangladesh",
    "BE": "Belgium",
    "BF": "Burkina Faso",
    "BG": "Bulgaria",
    "BA": "Bosnia and Herzegovina"
    }');

    //using jquery
    x=[]
    $.each(myObj, function(i,n) {
        x.push(n);});

     $.each(x, function (key, value) {
                         $("#opt_country").append($("<option></option>").val(key).html(value[key]));
                    });

//add following script to include jquery :
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.