Programmatically set the value of a Select2 ajax

Note this was tested with version 4+ I was finally able to make progress after finding this discussion: https://groups.google.com/forum/#!topic/select2/TOh3T0yqYr4 The last comment notes a method that I was able to use successfully. Example: $(“#selectelement”).select2(“trigger”, “select”, { data: { id: “5” } }); This seems to be enough information for it to match the ajax data, … Read more

Hide/Show Select2

I do a similar thing, however I hide the select2 container which is always the next node over from the start point so it would look like. $(document).on(‘change’, ‘.country’, function () { if ($(this).val() == $(this).data(‘current-countryCode’)) { $(‘#states’).next(“.select2-container”).show(); } else { $(‘#states’).next(“.select2-container”).hide(); } }); So I have been taking the same approach you have

Add class to select2 element

jQuery uses JSON objects to initialize, so I guess this one will as well:\ $(“#myBox”).select2({ containerCssClass : “error” }); If you want to add/remove a class you can do this after you initialized it $($(“#myBox”).select2(“container”)).addClass(“error”); $($(“#myBox”).select2(“container”)).removeClass(“error”); The above function gets the DOM node of the main container of select2 e.g.: $(“#myBox”).select2(“container”) Important You will need … Read more

select2 allowClear not enabled when options set dynamically

As stated in the doc, allowClear needs a placeholder and placeholder need a corresponding option value (which cannot be an empty string, but can be a single space). allowClear This option only works when the placeholder is specified. — placeholder Note that because browsers assume the first option element is selected in non-multi-value select boxes … Read more

How to remove selected option from the option list in select2 multiselect and show selected options in the order they are selected

Part #1 of Q: You can do a CSS trick to hide selected item like this: .select2-results__option[aria-selected=true] { display: none; } Part #2 of Q: Also you can do a JQuery trick to force selected items to end of tags box, ( by getting selected item on select, detach it (remove it), then reAppend it … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)