Skip to content Skip to sidebar Skip to footer

Filling Out Combobox Using Json Value

Hello I have a following html I have th

Solution 1:

Try this

var el = document.getElementById('fld_base_profile_id');

for(var i = 0; i < JSON.length; i++) {
    var profile = JSON[i],
        opt = document.createElement("option");

    opt.id = profile.id;
    opt.value = profile.name;
    el.appendChild(opt);
}​

Solution 2:

You could do it in a much cleaner way

Firstly make sure you are passing the Content-Type: application/json header in your JSON response - All of the Ajax methods in Prototype will automatically process the response as JSON and put it into transport.responseJSON

Then your javascript can clean up to this

this.baseProfile    = $("fld_base_profile_id");

vartype = transport.responseJSON.analyze_type;

type.each(function(item){
    var option = newElement('option',{'value':item.id}).update(item.name);
    this.baseProfile.insert(option);
},this);

Solution 3:

You can use JSON.Parse to turn the JSON you get into an object. then you can loop over the items, assign each one to an <option> object and then append them to the select.

JavaScript - populate drop down list with array explains how to create the option tags.

Post a Comment for "Filling Out Combobox Using Json Value"