How to get value of select2:unselect
Actually, the data value is inside the event Object. It would be useful if you are dealing with select2 multiple values.
function(e){
console.log(e.params.data)
}
This is an older question but maybe this answer will help someone. I'm using Select2 v4.0.3 with multiple values/tags and need only the ID of specific one being removed. I really did not want to use the unselecting
event as mentioned in other answers. In the unselect
event there is no args
object, so you can get to the ID of the one you are trying to remove like this...
jQuery('.mySelect2').on("select2:unselecting", function(e){
return true; // I don't use this unselecting event but here is how you could use it to get the ID of the one you are trying to remove.
console.log(e);
console.log(e.params);
console.log(e.params.args.data);
console.log(e.params.args.data.id);
//console.log(e.params.args.data.tag); data.tag is specific to my code.
});
jQuery('.mySelect2').on('select2:unselect', function (e) {
console.log(e);
console.log(e.params);
console.log(e.params.data);
console.log(e.params.data.id);
//console.log(e.params.data.tag); data.tag is specific to my code.
/*
Now you can do an ajax call or whatever you want for the specific
value/tag that you are trying to remove which is: e.params.data.id.
*/
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');