0

Code :

<select id="example">
    <option value="38">Marco</option>
    <option value="39">Hello</option>
</select>​

$("#example> option").click(function () {
    alert("ciao");
});

clicking on a drop down item, the alert is not showed... IE and Firefox no problems..

markzzz
  • 47,390
  • 120
  • 299
  • 507

3 Answers3

1

Use $('#example').change(...) - selecting/clicking an element will change the value of the selectbox so that should work.

ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
1

Use the change function instead - it is perfectly suited for drop-downs.

$('#example').change(function() {
    alert('ciao');
});
Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
1

it seems as though .change() is the correct way to go about this on the <select> element.

note, though, that you'll have to retrieve the option manually once a change event has been fired on the <select> since this refers to a <select> element and not an <option> element.

you'll have to do this: var $selected = $(this).find(":selected");

$selected will contain the select option, or multiple selected options if multiple select is enabled.

just want to clarify that .change() is to be used on the <select> element and not the <option> elements.

Dario Russo
  • 80
  • 1
  • 8