Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

listbox select issue

Tags:

jquery

I have the code provided below. I've tried using jquery to select to make something happen but eventually what I have doesnt work or may be incorrect.

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



<select id="emailList" multiple="multiple" name="emailList">
<option>[email protected]</option>
</select>

can someone provide me with the correct way of selecting an item from my listbox?

like image 235
hersh Avatar asked Nov 21 '25 17:11

hersh


2 Answers

Try:

$("#emailList").change(function() {
     alert($('option:selected', $(this)).text());
});
like image 97
Sarfraz Avatar answered Nov 24 '25 22:11

Sarfraz


You can use the .change() method like this:

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

Since your <option> has no value, the text will be the value, so using .val() works here. The .click() event doesn't execute on all browsers (IE...) for the <option> elements, so it's better to use .change().

like image 20
Nick Craver Avatar answered Nov 24 '25 22:11

Nick Craver



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!