Texbox is dynamically filled with a remote call using Select2 and how do I set preselected value. Here is the code
<input type="hidden" id="e6">
$("#e6").select2({
placeholder: "Search for a movie",
minimumInputLength: 1,
ajax: {
url: url,
dataType: 'jsonp',
data: function (term, page) {
return {
q: term, // search term
page_limit: 10, };
},
results: function (data, page) {
return {results: data};
}
}
});
I tried this to preselect value 1049
$('#e6').select2('val', '1049');
but this doesn't set the value on the textbox.
Any ideas?
in 2019 this worked for me
// Create a DOM Option and pre-select by default~
var newOption = new Option(data.text, data.id, true, true);
// Append it to the select
$('#mySelect2').append(newOption).trigger('change');
In case anybody wants to know how to solve this
$("#e6").select2('data', {id: '1049', text: 'MyLabel'});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With