0

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.

{"content":[{"value":"210","label":"xxx"},{"value":"340","label":"yyy"},{"value":"211","label":"zzz"}]}

My Jquery code

 success: function (response){
   alert(response[content]);
   }

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;
            }
0
jQuery.ajax({
    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>');
    });
});
0

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-select-id').html(optionHtml);
 }

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.