Get the last deselected value of a select multiple with JQuery [duplicate]

1

I have the following multi select:

<select multiple="multiple" id="select_empresas" name="my-select[]">
      <option value='elem_1'>elem 1</option>
      <option value='elem_2'>elem 2</option>
      <option value='elem_3'>elem 3</option>
      <option value='elem_4'>elem 4</option>
</select>

and the next JQuery:

$('#select_empresas').on('change',function(e) {
   var unselected = $(this).find('option:last:not(:selected)').val();
   alert(unselected);
});

What I want to do is show me the value of the last option deselected, for example if elem 2 is selected and then the user gives click on it and will go to deselect, at that moment is when I want get the value of said option , which would be elem_2 . But the problem is that it does not show me the value I want. If someone can help me I would appreciate it very much

    
asked by U.C 06.09.2018 в 14:45
source

1 answer

0

You can try something like this

<script>
$('#select_empresas option').on('click',function(e) {
    if($(this).is(":selected")){
        console.log($(this).text()+" Seleccionado");
    }else{
        console.log($(this).text()+" Deseleccionado");
    }
});
</script>

Greetings:)

    
answered by 06.09.2018 в 14:57