Questions tagged [jquery-select2]

Select2 is a jQuery-based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.

Select2 is a jQuery based replacement for select boxes. It supports searching, remote data sets, and infinite scrolling of results.

Stack Snippet Starter Pack

HTML

<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/css/select2.min.css" rel="stylesheet" />
<script src="//code.jquery.com/jquery-2.2.4.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.4/js/select2.min.js"></script>

<select class="select2">
  <option value="AL">Alabama</option>
  <option value="AK">Alaska</option>
  <option value="AZ">Arizona</option>
</select>

JavaScript

$('.select2').select2();

// get data when an option is selected
$('.select2').on('select2:select', function (e) {
    var data = e.params.data;
    console.log(data.id, data.text);

    // other data, eg get data-more:  
    $(data.element).data("more")
});

// language example changing no results
$('.select2').select2({ 
    language: { 
        noResults: function () { return "try again"; } 
    }
});

Useful links

Related Tags

5563 questions
2
votes
1 answer

jquery select2 plugin, loading in default with ajax selection

I am using the jquery select 2 plugin. http://ivaynberg.github.io/select2/#ajax I have the search working, and I am able to pull in the results and select things fine. But when I reload the page I want the previous selection populated. I have the id…
Wizzard
  • 12,582
  • 22
  • 68
  • 101
2
votes
0 answers

Querying database with select2 based on selection from previous select2

I'm working on a symfony project (using Doctrine as well) and I'd like to implement a staggered search on one of the pages, per example: User searches for an author in the first select2 box (which is pulling data from DB via Ajax), and once an item…
TuxMeister
  • 275
  • 1
  • 8
  • 20
2
votes
0 answers

jQuery: Select2 Not disabling previously chosen values

I have an array I am using select2 on but I would like the same values to be picked multiple times. Is there a way to enable this functionality? My code is below: #home.js $(document).ready(function(){ $("#productdropdown").select2({ …
barnett
  • 1,572
  • 2
  • 13
  • 25
2
votes
0 answers

select2 required and default notification

I;m using select2 with a required field. When the field is not filled out, in Chrome the notification that it's a required field is out of place. In FF, I can't even see the notification, so it just prevents the form from being submitted. any ideas…
Daniel
  • 34,125
  • 17
  • 102
  • 150
2
votes
1 answer

select2 with knockout change minimuminputlength dynamically

I am using Select2 control with knockout and I have a special requirement where I need to set the minimumInputLength option of the Select2 control dynamically based on the length of the options in the select2 combobox. For example if I have 100…
Krishna Veeramachaneni
  • 2,131
  • 3
  • 18
  • 24
2
votes
1 answer

how to set values when Select2 is ready?

Does Select2 have any onLoad event so that I can set some default value? Now I resorted to some ugly trick after Select2 is added to the Dom with the help of Ember's didInsertElement: `setTimeout(function() {$select.select2('val',…
wryrych
  • 1,765
  • 4
  • 20
  • 31
2
votes
0 answers

select2 changing positon of drop down list

I have used select2 as follows