hersh
hersh

Reputation: 1183

listbox select issue

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?

Upvotes: 3

Views: 3806

Answers (2)

Nick Craver
Nick Craver

Reputation: 630637

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().

Upvotes: 5

Sarfraz
Sarfraz

Reputation: 382909

Try:

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

Upvotes: 6

Related Questions