Monday, November 21, 2011

tie .keypress as an event handler to a particular element in jQuery

When using the up and down arrows to traverse the items in a dropdown list, the focus event handler will not fire and the blur event handler will to fire until you leave the dropdown list. How then may you fire events while moving from one list item to another?

$('#myDropdown').keypress(function (e) {

   switch (e.keyCode) {

      case 38:

         alert('you clicked the up arrow key');

         break;

      case 40:

         alert('you clicked the down arrow key');

         break;

      case 13:

         alert('you pressed return');

         break;

   }

});

No comments:

Post a Comment