I've got a simple Listbox on a HTML form and this very basic jQuery code
//Toggle visibility of selected item
$("#selCategory").change(function() {
$(".prashQs").addClass("hide");
var cat = $("#selCategory :selected").attr("id");
cat = cat.substr(1);
$("#d" + cat).removeClass("hide");
});
The change event fires fine when the current item is selected using the Mouse, but when i scroll through the items using the keyboard the event is not fired and my code never executes.
Is there a reason for this behavior? And what's the workaround?
I had this problem with IE under JQuery 1.4.1 - change events on combo boxes were not firing if the keyboard was used to make the change.
Seems to have been fixed in JQuery 1.4.2.
The
onchange
event isn't generally fired until the element loses focus. You'll also want to useonkeypress
. Maybe something like:You'll want both
onchange
andonkeypress
to account for both mouse and keyboard interaction respectively.Sometimes the change behavior can differ per browser, as a workaround you could do something like this:
You can chain whatever events you want and manually fire the change event.
IE:
The behavior you describe, the change event triggering by keyboard scrolling in a select element, is actually an Internet Explorer bug. The DOM Level 2 Event specification defines the
change
event as this:If you really want this behavior, I think you should look at keyboard events.
Check a example here...