Reputation: 51
{"names": [
{"patientName": "Ratna"},
{"patientName": "raju" },
{"patientName": "krishna"},
{"patientName": "kishore"},
{"patientName": "Kishore1"},
{"patientName": "mahesh"}
]}
this is the JSON object i'm getting from Ajax call
so now i want to add all patientName
values to select box through jquery
can any one tell me how to accomplish this ??
here i'm using $.ajax() function for ajax call
thanks in advance
Upvotes: 0
Views: 580
Reputation: 28995
Try this,
var data = {
"names": [
{"patientName": "Ratna"},
{"patientName": "raju" },
{"patientName": "krishna"},
{"patientName": "kishore"},
{"patientName": "Kishore1"},
{"patientName": "mahesh"}
]
}
var names = data.names;
var options = [];
for(i=0,len=data.names.length;i<len;i++){
options[i] = '<option>' + names[i].patientName + '</option>';
}
$('<select></select>').append(options.join('')).appendTo('body');
Upvotes: 0
Reputation: 97672
var select = $('#selectid');
$.each(data.names, function(i, v){
select.append('<option value="'+v.patientName+'">'+v.patientName+'</option>');
}
Upvotes: 1
Reputation: 187
Try something like:
var selectbox = '';
var options = '';
for (var i = 0; i < YourJsonObject.names.length; i++) {
options += '<option value="' + YourJsonObject.names[i] + '">' + YourJsonObject.names[i] + '</option>';
}
$("select#yourSelectBoxID").html(selectbox);
Upvotes: 0