List selection

I have the code below. I tried using jquery to choose something to happen, but in the end, something that doesn't work for me or might be wrong.

$("#emailList option").click(function() {
     alert("OMG");
});



<select id="emailList" multiple="multiple" name="emailList">
<option>abc@123.com</option>
</select>

can someone provide me with the correct way to select an item from my list?

+3
source share
2 answers

Try:

$("#emailList").change(function() {
     alert($('option:selected', $(this)).text());
});
+6
source

You can use the method .change():

$("#emailList").change(function() {
  alert("Current value:" + $(this).val());
});

Since yours <option>doesn't matter, the text will be the value, so it is used here .val(). The event is .click()not executed in all browsers (IE ...) for elements <option>, therefore it is better to use .change().

+5
source

Source: https://habr.com/ru/post/1761931/


All Articles