Search code examples
jqueryajaxjquery-select2-4

select2 changing value dynamically using jquery


I have searched many answers and tried them but i am not able to change the value of select2 dropdown dynamically using jquery.

I am using the ajax select2 getting the data remotely using json.

dropdown is working fine.

Here is what i have tried so far.

//UserGroupID is a variable contains the database value
var groupSelector = $("#EditSpeciality");
//1.
groupSelector.select2("val", {ID: userGroupID, TEXT: "Lorem Ipsum"});
//2.
groupSelector.val(userGroupID).trigger("change");
//3.
groupSelector.select2("val", {ID: userGroupID, TEXT: "Lorem Ipsum"});
//4.
groupSelector.val(userGroupID);

But none of them seems to be changing the dropdown value.

How to dynamically set the value..

Here is my HTML.

<select class="form-control select2" id="groupSelector" name="groupSelector" style="width: 100%;"></select>

=-=-=-=-=-=-=-=-=-=-=

Update:

Im using codeigniter MVC, i know this should not matter. but want to put everything on the table here.

The version of select2 is 4.0.0

JSON code that i am getting is from Controller through which select2 is populating the list.

Below is select2 Code.

function commonSelect2(selector,url,minInputLength,placeholder){
    selector.select2({
        minimumInputLength:minInputLength,
        placeholder:placeholder,
        ajax: {
            url: url,
            dataType: "json",
            delay: 250,
            data: function (params) {
                return {
                    q: params.term, // search term
                    page: params.page
                };
            },
            processResults: function (data) {
                return {
                    results: $.map(data, function(obj) {
                        return { id: obj.ID, text: obj.TEXT };
                    })
                };
            },
            cache: true
        },
        debug:false
    });
}

i call the above function like this to initilize the dropdown in page.

 // Speciality Selector for editing popup box
var SelectorSpeciality = $("#EditSpeciality");
var minInputLength = 0;
var placeholder = "Select Speciality";
var ConsultantSelectorURL = "' . base_url() . 'Admin/select_speciality";
commonSelect2(SelectorSpeciality,ConsultantSelectorURL,minInputLength,placeholder);

the function resides in some .js file under assets/js directory.

it helps me have the clean code and call the function whenever i want function.

i think we can fix it with initselection but i don't know how to fix it with initselection i also did tried googling it but had no luck..


Solution

  • As long as the option is appended to the list first, the second option will work (for select2 4.0)

    $("#groupSelector").append ('<option value="' + userGroupID+ '">' + userGroupID+ '</option>')
    
    groupSelector.val(userGroupID).trigger("change"); 
    

    If you need the option to be selected this has to be done when appending,

    $("#groupSelector").append ('<option selected="selected" value="' + userGroupID+ '">' + userGroupID+ '</option>')
    

    and the trigger change isn't needed