Using Select2 get error – “Error: No select2/compat/query”

You are running into two issues here, both of which can easily be fixed. Select2 4.0.0 no longer supports the query option in the slimmed down, standard build. This must be included in the full build (select2.full.js) as it is handled through a backwards compatibility module. You are using Placecomplete and it depends on an … Read more

Dynamically add item to jQuery Select2 control that uses AJAX

This is a lot easier to do starting in select2 v4. You can create a new Option, and append it to the select element directly. See my codepen or the example below: $(document).ready(function() { $(“#state”).select2({ tags: true }); $(“#btn-add-state”).on(“click”, function(){ var newStateVal = $(“#new-state”).val(); // Set the value, creating a new option if necessary if … Read more

JQuery Select2 – How to select all options

using Select 2 DEMO $(“#e1”).select2(); $(“#checkbox”).click(function(){ if($(“#checkbox”).is(‘:checked’) ){ $(“#e1 > option”).prop(“selected”,”selected”);// Select All Options $(“#e1”).trigger(“change”);// Trigger change to select 2 }else{ $(“#e1 > option”).removeAttr(“selected”); $(“#e1”).trigger(“change”);// Trigger change to select 2 } }); $(“#button”).click(function(){ alert($(“#e1″).val()); }); <select multiple id=”e1″ style=”width:300px”> <option value=”AL”>Alabama</option> <option value=”Am”>Amalapuram</option> <option value=”An”>Anakapalli</option> <option value=”Ak”>Akkayapalem</option> <option value=”WY”>Wyoming</option> </select> <input type=”checkbox” id=”checkbox” >Select All … Read more

How to use placeholder as default value in select2 framework

You have to add an empty option (i.e. <option></option>) as a first element to see a placeholder. From Select2 official documentation : “Note that because browsers assume the first option element is selected in non-multi-value select boxes an empty first option element must be provided (<option></option>) for the placeholder to work.” Example: <select id=”countries”> <option></option> … Read more