0

I have a select multiple list:

<select id="List" multiple="multiple">  
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>

I trigger the event on the select:

$("select#List").change(function() {
    // do ajax stuff...
});

I need to get the value of the clicked option that fired the change event.

Example: if the option 1 is already selected, and I select the "option 2" that triggers the .change() event, I want get the value of "option 2".

(I've already read this post Get clicked option in multiple dropdown, but it doesn't work for me).

Community
  • 1
  • 1
user2385964
  • 21
  • 1
  • 1
  • In what way does it 'not work for you'? Because those are the only, and best, practical solutions I can think of with jQuery. Without explanation of why this question isn't a duplicate, of the one you link to in the question, I'm voting to put on hold as a duplicate of that question. – David Thomas Aug 27 '14 at 10:53
  • You will get all you want in here [jQuery .change](http://api.jquery.com/change/) (last examples on that page). – rails_id Aug 27 '14 at 10:56
  • I've placed your question on hold in order to prompt you to improve your question and detail the way(s) in which the duplicate question (to which you linked) fails to answer your question. – David Thomas Aug 27 '14 at 11:35

1 Answers1

2

What does not work for you, I've checked the answers in link which you posted and it works.

See working example on fiddle: http://jsfiddle.net/unkxt8h3/

The only thing here is, that the event is fired even when you deselect the option. But checking if it's selected or not should be easy peacy ;)

html:

<select id="List" multiple="multiple">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
</select>

JS:

$("#List option").click(function () {
    var value = $(this).attr("value");
    console.log(value);
    alert(value);
});
Tomor
  • 654
  • 5
  • 10