select an option select2 code example
Example 1: select2 add option
if ($('#mySelect2').find("option[value='" + data.id + "']").length) {
$('#mySelect2').val(data.id).trigger('change');
} else {
var newOption = new Option(data.text, data.id, true, true);
$('#mySelect2').append(newOption).trigger('change');
}
Example 2: select2 replace options
var options = [];
$.each(dataReturn, function (i, obj) {
options.push({
text: obj.NickName,
id: obj.NickName
});
})
$("#BeneType").empty().select2({
data: options
});