Select2 open dropdown on focus

Working Code for v4.0+ *(including 4.0.7) The following code will open the menu on the initial focus, but won’t get stuck in an infinite loop when the selection re-focuses after the menu closes. // on first focus (bubbles up to document), open the menu $(document).on(‘focus’, ‘.select2-selection.select2-selection–single’, function (e) { $(this).closest(“.select2-container”).siblings(‘select:enabled’).select2(‘open’); }); // steal focus during … Read more

Select2: Hide certain options dynamically

Would adding the following CSS Rule to the page solve your problem? .select2-container–default .select2-results__option[aria-disabled=true] { display: none; } Basically it would hide a disable option instead of displaying it with a gray background. Use disabled instead of display:’none’ in your options list also. JS Bin

Select2 limit number of tags

Sure, with maximumSelectionLength like so: $(“#tags”).select2({ maximumSelectionLength: 3 }); Maximum Selection Length Select2 allows the developer to limit the number of items that can be selected in a multi-select control. http://ivaynberg.github.io/select2/ It has no native callback, but you can pass a function to formatSelectionTooBig like this: $(function () { $(“#tags”).select2({ maximumSelectionLength: 3, formatSelectionTooBig: function (limit) … Read more

Update select2 data without rebuilding the control

select2 v3.x If you have local array with options (received by ajax call), i think you should use data parameter as function returning results for select box: var pills = [{id:0, text: “red”}, {id:1, text: “blue”}]; $(‘#selectpill’).select2({ placeholder: “Select a pill”, data: function() { return {results: pills}; } }); $(‘#uppercase’).click(function() { $.each(pills, function(idx, val) { … Read more