dens
dens

Reputation: 1

How to select multiple values in form:select using ajax response

I have an Ajax call that will return an object with an attribute of collection of profile. How can I select multiple values in <form:select> using these collection of profile.

$.ajax({
        ... // some omitted codes // ...

        success : function(response) {
            response.profiles // collection of profiles
        }


<form:select id="profile" path="profiles"
      items="${profilelist}" multiple="true" itemValue="id" itemLabel="type"/>

Upvotes: 0

Views: 454

Answers (1)

Darshan Thakkar
Darshan Thakkar

Reputation: 234

This seems duplicate question, always first try to see if similar question is there for your problem.

You also have not specified which framework you are using, because if you are using any framework then you can get data in option based on that.

Here I am answering for without any framework,

<form:select id="profile" path="profiles"
      items="${profilelist}" multiple="true" itemValue="id" itemLabel="type"/>

ajax call should be something like this,

    $.ajax({
       type: "GET",
       url: "http://.......api/1/AbsenceType",
       dataType: "json", 
       success: function (data) {

        // Get select
       var select = document.getElementById('profiles');

    // Add options
      for (var i in data) {
         $(select).append('<option value=' + data[i] + '>' + data[i] + 
  '</option>');

    // Set selected value
    $(select).val(data[1]);
    }
        }

Hope this helps.

Upvotes: 1

Related Questions