Fawzan Izy
Fawzan Izy

Reputation: 1850

Populate dropdown using json

I need to know how to populate a dropdown using the JSON object.. Im using php to send json to the client.. client catches the json data using ajax. i use below code to check what i exactly receive from the server.

  var test = xmlhttp.responseText;
  alert(test);

the result was showing me.. as below...

   {"2":"pricelist.xml","3":"camera.xml", "4":"cd.xml","5":"data.xml"}

what i exactly do is.. i have set of xml files on the server im reading those filenames using php and i convert it to json object using json_encode and then i send that ot the client to populate the dropdownlist using the json data which i send from server. I have no issue in receiving the json data in client side as i tested above. i need to know how do i populate the data using above json data

Upvotes: 0

Views: 2251

Answers (1)

Juzer Ali
Juzer Ali

Reputation: 4167

Your HTML

<select id="myselect">
</select>

The javascript(Using jQuery)

var json={} // Populate this json object
$.each(json, function(key, value){
    $('#myselect').append("<option value='"+key+"'>"+value+"</option>");
});

Upvotes: 1

Related Questions