Select2 Load On Demand Dropdown set selected value in Jquery

1.2k views Asked by At

I am using Load On Demand Dropdown in Select2. I need to set the dropdown value selected using jquery. I was trying the below code but It doesnt seems to be working. Anybody have any idea about how to achieve this.

Code

$("#ddl").select2("data", { id: 1, text: "India" });
1

There are 1 answers

0
DonMax On BEST ANSWER

I found the way to set the selected value for Select2 remote dropdown. Use the below code to make it working.

Code

var $element = $("#dropdown ID");
var option = new Option(value, id, true, true);

// Append it to the select2 dropdown element
$element.append(option).trigger("change");