pnj Posted July 1, 2007 Share Posted July 1, 2007 I want to disable keyboard scrolling for drop down boxes, because people are inadvertently changing the field when they want to scroll the page. The code below works in IE, it does not work in Firefox. Can you tell me why? I know the cb() function is returning false, even in Firefox, but the combobox scrolls anyway. Thoughts? <script> function cbHandleKeyDown(e) { var keynum; /* get keystroke number */ if (window.event) { /* IE */ keynum = e.keyCode; } else if (e.which) { /* Netscape / Opera / Firefox */ keynum = e.which; } /* reject arrow keys,home,end,pgup,pgdown - everything that could move the selection. */ if (keynum > 32 && keynum < 41) { return false; } /* pass other keys through, so it is still possible to navigate with letters*/ return true; } </script> <select onkeydown="return cbHandleKeyDown(event);"><option>...</option></select> Thanks Quote Link to comment https://forums.phpfreaks.com/topic/57975-rejecting-arrow-keys-with-onkeydown-in-firefox/ Share on other sites More sharing options...
roopurt18 Posted July 6, 2007 Share Posted July 6, 2007 A few things: 1) You are testing the existence of window.event to determine if the browser is IE, which is fine, but then you are assigning the keyCode from e.keyCode and e will be undefined in IE. 2) You are not canceling the default action correctly. 3) I can't remember the details because my example is on a PC at home, but FF, Netscape, and Opera do not set the which property the same, so you may need to do additional testing there. Anyways, try this: function cbHandleKeyDown(e) { e= e || window.event; var keynum; /* get keystroke number */ if (window.event) { /* IE */ keynum = e.keyCode; } else if (e.which) { /* Netscape / Opera / Firefox */ keynum = e.which; } /* reject arrow keys,home,end,pgup,pgdown - everything that could move the selection. */ if (keynum > 32 && keynum < 41) { if(window.event){ e.returnValue = false; }else{ e.preventDefault(); } return false; } /* pass other keys through, so it is still possible to navigate with letters*/ return true; } Quote Link to comment https://forums.phpfreaks.com/topic/57975-rejecting-arrow-keys-with-onkeydown-in-firefox/#findComment-291610 Share on other sites More sharing options...
pnj Posted July 7, 2007 Author Share Posted July 7, 2007 awesome, thanks! -pnj Quote Link to comment https://forums.phpfreaks.com/topic/57975-rejecting-arrow-keys-with-onkeydown-in-firefox/#findComment-292182 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.