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

Asked

Viewed 1,322 times

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 <option> selected:

<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 (know that on('click', func..) will not work in this case), regardless of whether it is being selected or deselected.

I did a lot of research, but the most I could find was how to find the value of the last selected.

The plugin used is the Bootstrap-multiselect

  • 1

    I’m not sure if this is what you want, but look at this Jsfiddle with an example of what seems to me to be what you seek.

  • @Zull, the concept is exactly this. I read the plugin documentation, and adapted. Thank you for helping.

3 answers

6

Try to use 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)
  */
});

0

To achieve the effect you expect, you need to put a Handler for each element option:

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

Example of code in operation:

http://jsfiddle.net/ryNFE/2/

Example with console.log (without Alert):

$("#multiselect").on("click", "option", function() {
    var clickedOption = $(this);
    console.log(clickedOption.val());
});
  • In this code if you hold the ctrl to uncheck the item, it will give the alert also.

  • @Gabrielgartz then you need to change your question: "But what I really wanted was to get the value of the <option> clicked (I know that on('click', func.. ) will not work in this case), independent whether it is being selected or desiccated."

  • @Gabrielgartz then could not understand what the problem, if the solution returns exactly what you asked in the question.

  • 2

    @Oniltonmaciel Question asked by Eric Vieira. I think Gabriel Gartz is merely alerting you to a detail in your solution that could cause some inconvenience.

  • 1

    Okay, my mess, I thought it was the author. Thanks for the @Zuul clarification. Anyway, just to clarify, holding Ctrl here (for me at least), does not cause the click event by itself. Unless you do Ctrl+Click. But in this case you are performing a click and action must be activated, "regardless of whether the item is being selected or deselected", as requested in the question.

  • Person, I managed to resolve. I apologize for not having informed of face which plugin was using. Anyway, the concept was exactly the one presented by @Oniltonmaciel and Zuul

Show 1 more comment

0


RESOLVED

Guys, thanks for your attention. The plugin I’m using is the Bootstrap-Multiselect. I do not know if there is a more elegant and better solution, but, I managed to solve so:

 $(document).ready(function() {
  $('#multiselect').multiselect({
    onChange: function(element, checked) {
      console.log(element[0]['value']);
    }
  });
});
  • You can mark your answer as accepted there on the left side.

  • Only after two days. =)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.