Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select2: How to get previous selected values

I am using select v4.0.3. How do I get the replaced/previous value of the select element? I already attached a 'change' listener but I can't seem to find the previous value.

like image 269
Emmanuel Campos Avatar asked Sep 08 '16 03:09

Emmanuel Campos


2 Answers

Previously selected value can be acquired by using select2:selecting event

see the codes here: https://codepen.io/jacobgoh101/pen/ZpGvkx?editors=1111

$('select').on('select2:selecting', function (evt) {
  console.log('previously selected ' + $('select').val());
});
$('select').on('select2:select', function (evt) {
  console.log('now selected ' + $('select').val());
});
like image 183
Jacob Goh Avatar answered Sep 20 '22 23:09

Jacob Goh


You can do it easily in selecting event

$(this).on("select2-selecting", function (e) {
    var curentValue = e.val;
    var previousValue = $(this).val();
});
like image 33
vborutenko Avatar answered Sep 17 '22 23:09

vborutenko