tags:

views:

31

answers:

2

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?

+1  A: 

Try:

$("#emailList").change(function() {
     alert($('option:selected', $(this)).text());
});
Sarfraz
@ Sarfraz -- thanks for the help.
hersh
+1  A: 

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

Nick Craver
@Nick -- do you have any reason why when I select just one item from the list box, it is giving me all the text item instead of the one I selected? For example, I should only be able to select one, not multiple and when I select what I want, only what I select will show in a textbox I have on that same page.
hersh
@hersh - The `multiple="multiple"` is what allows multiple selectors, just remove it to make it a single select.
Nick Craver
@Nick -- how would I remove this? Thanks in advance.
hersh