views:

97

answers:

2

Hello,

I would like to fire an event when a select box is updated,

I've tried with change() but this function only work when the user use his mouse to select an option in the selec box.

So when I choose option using his keyboard (arrow or first letter of the option element) nothing happen =/

A solution was to use keydown() but as long as an$ option is not really selected I can't get the current value (the focused option element)

Does anyone know how to solve this ?

+1  A: 

The change event should fire also when the keyboard is used. However this won't happen until the 'Enter' key is pressed. The value doesn't change when the arrow keys are pressed. The user needs to confirm the selection by pressing enter.

kgiannakakis
+1... also tabbing away from the selector should trigger the change event. But I remember reading somewhere that IE behaves differently - I found this SO answer (http://stackoverflow.com/questions/1533618/jquery-selector-problem-in-internet-explorer) but I don't think that was the problem.
fudgey
A: 

If you mean you want to fire an event when the user selects an option from a dropdown, just use something like the following:

$(document).ready(function(){

       $("#myElement").change(function() {
   alert($("#myElement").val());
  }).keyup(function() {
   alert($("#myElement").val());
  }); 

});

This will check for both user inputs (mouse and keyboard) and fire an alert (or whatever you wish) when it changes.

Gearóid