0
{"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

3 Answers 3

1
var select = $('#selectid');
$.each(data.names, function(i, v){
    select.append('<option value="'+v.patientName+'">'+v.patientName+'</option>');
}

Doc - $.each(), .append()

Fiddle

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

Comments

0

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);

Comments

0

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');

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.