<...">

Get click option in multiple drop-down lists

I have a dropdown with several choices, for example:

<select id="myList" multiple="multiple">  
    <option value="1">Opt #1</option>
    <option value="2" selected="selected">Opt #2</option>
    <option value="3" selected="selected">Opt #3</option>
    <option value="4">Opt #4</option>
</select>

If I then choose Opt #4, then how can I receive Opt #4, and not Opt #2and then Opt #3? I know that I can get all selected options:

var selectedOptions = $("#myList option:selected");

However, I only need the option that I pressed - Opt #4. Is it possible?

Edit: note that when manipulating a list inside an event, changeI cannot do this in the event click. Also added missing few.

+5
source share
3 answers

You can get it in the click handler for each option element:

$("#myList option").click(function() {
    var clickedOption = $(this);
});

Update

EDIT: , .

, on. :

$("#myList").on("click", "option", function() {
    var clickedOption = $(this);
});
+7

, № 4 Cntrl, №4 .

№ 4 Cntrl, . , . №4, .

+1

:

$('#myList').delegate('option', 'click', function (opt) {
  alert('Option ' + opt.value + ' was clicked');
});
+1

All Articles