I use select2 dropdown and just upgraded to version 4. One function that seems to be violated (or I'm doing something wrong) is the allowClear parameter when you have a remote data source. And select2 sits in the jquery user interface dialog
It works fine if the data source is local (no ajax) or if there is no jquery UI dialog box, but when I try to use the remote data source in the jquery user interface dialog box, I see problems.
Interestingly, when seetting allowClear = true, "X" DOES is displayed on the right side of the drop-down list after I find the search and select the item, but when I click it, all of the select2 code works, but the previous selected value is not deleted. I was debugging select2.js code, and all the code that seems like it should be running (handleClear, etc.) seems to work properly.
When testing in a local example, I see that this line of code actually removes the value:
this.$element.val(this.placeholder.id).trigger('change');
when I debug the same code on a remote data source, nothing changes when this line of code fires.
From googling, the only related problem that I see if you did not specify placeholder text ( example question here ) but in my case (as indicated below) I indicate this
Here are some screenshots:


and here is my code below that sets this select2 dropdown menu:
asp.net-MVC View:
<% = Html.DropDownList("MyDropdownId", Model.DropdownItems, new {@id = "MyDropdown"}) %><br/><br/>
and here is my javascript:
$("#MyDropdown").select2({ theme: "classic", width: "280px", allowClear: true, ajax: { url: "/MyController/Search", dataType: 'json', delay: 300, data: function (params) { return { q: params.term }; }, processResults: function (data, params) { return { results: data }; }, cache: false }, escapeMarkup: function (markup) { return markup; }, minimumInputLength: 3, placeholder: "Search", templateResult: streamFormatResult, templateSelection: streamSelectionResult });
Has anyone else encountered this problem or can understand why clicking on the "X" in the select2 drop-down menu with a remote data source will not eliminate the previously selected selection?
Update
Given Kevin's answer below, the problem seems to be isolated from the fact that my select2 selection is in the jquery user interface dialog box (as if I was trying to use it on a regular page, it works properly)
the only other code I had to add (to get select2 working in the jquery user interface dialog) was this code:
if ($.ui && $.ui.dialog && $.ui.dialog.prototype._allowInteraction) { var ui_dialog_interaction = $.ui.dialog.prototype._allowInteraction; $.ui.dialog.prototype._allowInteraction = function (e) { if ($(e.target).closest('.select2-dropdown').length) return true; return ui_dialog_interaction.apply(this, arguments); }; }
Without the code above, select2 input is not editable. I am not sure if this causes the problem to not clear the previous selected value when I click on "X".