Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
130
rated 0 times [  132] [ 2]  / answers: 1 / hits: 33915  / 9 Years ago, fri, december 25, 2015, 12:00:00

how can I get value of unselected option in Select2 using select2:unselect



$('#mySelect').on(select2:unselect, function(e){

var unselected_value = $('#mySelect').val(); // using this shows 'null'
// or using below expression also shows 'null'
var unselected_value = $('#mySelect :selected').val();

alert(unselected_value);
}).trigger('change');


in above code alert shows 'null'



I need to use select2:unselect because 'change' event will sense :select and :unselect both.


More From » jquery

 Answers
4

Finally, I've got the solution and that is:
The value of unselected option is only available before it is unselected. Not at select2:unselect rather at select2:unselecting
now the code will be:



$('#mySelect').on(select2:unselecting, function(e){
var unselected_value = $('#mySelect').val();
alert(unselected_value);
}).trigger('change');

[#63946] Wednesday, December 23, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
longd

Total Points: 616
Total Questions: 110
Total Answers: 101

Location: Andorra
Member since Sat, May 27, 2023
1 Year ago
;