I got a json response for a dropdown field from a controller, But how to I append the options to the dropdown, Please provide me a solution. I got response like below.


My Jquery code

 success: function (response){

How do I loop the values and update in the dropdown Please provide me a solution

For Reference

I am trying to convert the below prototype js code to jquery code

onSuccess: function (response) {
                let options = response.responseText.evalJSON();
                let opt = "<option value=''>-- Please select --</option>";
                options.each(function (option) {
                    opt += "<option value='" + option.value + "'>" + option.label + "</option>";
                document.getElementById("option_id").innerHTML = opt;
    url: "your-url"
}).done(function(response) {

    jQuery("#your-select-id").append('<option value="">--Please select--</option>');

    jQuery.each(response,function(key,value) {
        jQuery("#your-select-id").append('<option value="'+value.value+'">'+value.label+'</option>');

Try This, This will work

 success: function (response){
    var content = JSON.parse(response).content;
    var optionHtml;
    for(var val of content){
      optionHtml +='<option value="'+val.value+'">'+val.label+'</option>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.