On click update value of SELECT2 AJAX dropdown

Ask a question+
0

I’m using Select2 Dropdown library
On tag dropdown there is select2 on ajax is applied

$('#sug_cstm_tag').select2({ ajax: {
                    url: base_url+'index.php',
                    dataType: 'json',
                    delay: 250, //wait for 250 milliseconds for another trigger
                    data: function (params) {
                          var query = {
                            entryPoint:'restapi',
                            module:'vaidam',
                            action:'newtags',
                            search: params.term,
                            type:'include_id'
                          }
                          return query;
                        }
                    },
                    placeholder: 'Search Tags',
                    minimumInputLength: 2,
                    allowClear: true,width: "140px"
                });

Above code manipulate select options with sug_cstm_tag ID

What I require is on Click an external button to update the selected option value.
Which is easily applied on normal select2 initialization
e.g. $('#sug_cstm_tag').select2();
value change $('#sug_cstm_tag').val('fibroids').trigger('change');

add comment

1 Answer

0

Found The Solution
First create Option Object with values
Then append it to select option and trigger change

var option = new Option(tag_text_value, tag_id, true, true);
studentSelect.append(option).trigger('change');

See in detail here

Ashutosh200 80
add comment

Your Answer