is it possible to know when a selected element is deleted in select2 with jquery? [closed]

0

I have a select2 with several options selected, the question is, when I remove one of those elements can I know with jquery the event or how to know that the element is being removed?

    
asked by roman madrigal 04.11.2016 в 06:40
source

1 answer

0

in the select2 documentation there is a section of programatic control that shows you how you can detect the change in the selections, the ones that interest you are two select2:select and select2:unselect , detects the selection and detects the deselection of an option

I'll give you an example:

//creamos el select2
$('select').select2();
//detectamos que opcion se selecciono
$('select').on("select2:select", function (e) { 
  console.log(e.params.data);
});
//detectamos se opcion se quito funciona con select multiple
$('select').on("select2:unselect", function (e) { 
  console.log(e.params.data);
});
select{
  width:200px
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link href="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/css/select2.min.css" rel="stylesheet" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/select2/4.0.3/js/select2.min.js"></script>
<select multiple>
  <option></option>
  <option value="0">0</option>
  <option value="1">1</option>
  <option value="2">2</option>
  <option value="3">3</option>
  <option value="4">4</option>
  <option value="5">5</option>
</select>
    
answered by 04.11.2016 / 07:46
source