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

5

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_with%:

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

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

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

But what I really wanted was to get the value of <option> clicked (I know <option> 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

    
asked by anonymous 11.02.2014 / 15:50

3 answers

0

RESOLVED

Personal, thank you for your attention. The plugin I am using is the Bootstrap-Multiselect. I do not know if there is a more elegant and better solution, but I have managed to solve like this:

 $(document).ready(function() {
  $('#multiselect').multiselect({
    onChange: function(element, checked) {
      console.log(element[0]['value']);
    }
  });
});
    
11.02.2014 / 18:58
6

Try using it 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)
  */
});
    
11.02.2014 / 16:47
0

To get the effect you expect, you need to put a handler for each option element:

$("#multiselect").on("click", "option", function() {
    var clickedOption = $(this);
    alert(clickedOption.val());
});

Example of working code:

link

Example with console.log (without alert):

$("#multiselect").on("click", "option", function() {
    var clickedOption = $(this);
    console.log(clickedOption.val());
});
    
11.02.2014 / 16:33