AjaxLoser
AjaxLoser

Reputation: 27

Select2 trigger on click

I have seen many topics about select2 trigger but I can't find a solution to my case. I want my select2 filter to load some data immediately when a user click on it.

Is that possible ? I tried to do it, it only works when a another option is selected, not at the moment when the select is clicked.

Thanks !

For example, this works for change trigger :

$('#id_station').on('select2:select', function (e) {
   alert();
});

Upvotes: 1

Views: 7224

Answers (1)

Manasi
Manasi

Reputation: 765

I am using select2 version 4.0.3 and the following code is working for me

   $('#id_station').on('select2:select', function (e) {
   alert();
});


please check below example

(function($){
  $('#id_station').select2();
  
 
  $('#id_station').on('select2:select', function (e) {
   alert("Selected");
});

})(jQuery);
body{
  font-family: sans-serif;
}

.select2{
  width: 100%;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.full.min.js"></script>

<select class="select2" id="id_station">
  <option value="1">Option 1</option>
  <option value="2">Option 2</option>
  <option value="3" selected>Option 3</option>
  <option value="4">Option 4</option>
  <option value="5">Option 5</option>
  <option value="6">Option 6</option>
  <option value="7">Option 7</option>
</select>

Upvotes: 1

Related Questions