I have a single selection select2 that uses an ajax search to find results. I have initialized it like this:
Task.ConfigureAssignee = function (objId) {
var sectionID = (Utilities.GetQueryStringParams('subsection') != undefined) ? Utilities.GetQueryStringParams('subsection') : "-1";
$(objId).select2({
placeholder: 'Type names of people...',
minimumInputLength: 3,
multiple: false,
width: '520px',
id: function (obj) {
return obj.ID;
},
ajax: {
type: "POST",
datatype: 'json',
url: "/webservices/globalwebservice.asmx/EventInviteSearch",
data: function (term) {
return JSON.stringify({ q: term, sectionId: sectionID });
},
contentType: 'application/json; charset=utf-8',
results: function (data, page) {
return { results: data.d.SearchResults };
}
},
formatResult: Task.FormatPersonSearchResult,
formatSelection: Task.PersonForSelection
})
}
What i would like to do is on a button click add a new item into the select via javascript.
Ive looked at $("#ddlinput").select2("val", "CA"); from the documenation, but this requires the item to be alreayd be bound to the select2.
Any help would be great.
I think I may need to set the value of the input and recreate the select2, using initSelection function. I tried this and it doesn't work however. No errors, but nothing happens.