javascript – Get the value of the "option" that was clicked either to select/deselect

Question:

I have the following <select> :

<select id="multiselect" multiple="multiple">
  <option value="pedra">Pedra</option>
  <option value="papel">Papel</option>
  <option value="tesoura">Tesoura</option>
</select>

With the Jquery code below, I can get the value of the last selected <option> :

<script type="text/javascript">
  $(document).ready(function() {
    $('#multiselect').multiselect();

    $(document).on('change','#multiselect', function(){

        console.log (( $('option', this).filter(':selected:last').val() ))
    });
  });
</script>

However, what I really wanted was to get the value of the clicked <option> (I know that on('click', func..) will not work in this case), regardless of whether it is being selected or deselected.

I searched a lot, but the most I found was how to find the value of the last one selected.

The plugin used is Bootstrap-multiselect

Answer:

Try using like this:

$('#multiselect').multiselect();
$("#multiselect").on("multiselectclick", function (event, ui) {
    console.log(ui.value);
});

Example

I found this in the documentation :

$("#multiselect").on("multiselectclick", function(event, ui) {
  /* event: the original event object
     ui.value: value of the checkbox
     ui.text: text of the checkbox
     ui.checked: whether or not the input was checked or unchecked (boolean)
  */
});
Scroll to Top