Hüseyin Okumuş
Hüseyin Okumuş

Reputation: 157

How to set select value in select2 plugin - jquery

I use this code for insert data to select element with select2 plugin:

$.ajax({
    type: "POST",
    url: "ws.asmx/GetEvrakGrup",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (dc, status) {
        jsonData = JSON.parse(dc.d);
        $("#selectId").select2({
            data: jsonData
        });

    },
    error: function () { alert("This is an Error");}
});

After I want to set a value in this select but its not working:

$("#selectId").val(81);

Upvotes: 5

Views: 28726

Answers (3)

amal50
amal50

Reputation: 1011

$("#MyDropdownList").select2("trigger", "select", {
                        data: { id: theID, text: theText }
                    });

Upvotes: 6

Joe. L
Joe. L

Reputation: 407

Try this :

$("#selectId").val(81).trigger('change');

Instead of

$("#selectId").val(81);

Upvotes: 18

Prashant Patil
Prashant Patil

Reputation: 2573

try below code:

var $example = $("#selectId").select2();
$example.val(81).trigger("change"); 

Upvotes: 5

Related Questions