Stop scrolling window with coded key (arrow), event.preventDefault () not working?
I built autosuggest and the keycode works to move up and down the list, but it scrolls the window. I tried event.preventDefault () but that doesn't stop it. Any ideas? This is what I tried:
$(document).keyup(function(e) {
e.returnValue=false;
e.preventDefault();
switch(e.keyCode) {
case 40:
suggestionLine++;
$('#suggestionLine_'+suggestionLine).focus();
break;
// etc...
Thank!
+3
Ben Kirchner
source
to share
1 answer
You need keydown
, not keyup
.
Why? The default operation you are trying to prevent happens immediately when you press a key (try it now!). This allows functions such as autorepeat keydown
to dispatch multiple events before dispatching a single event keyup
. By now keyup
, the scrolling has already been completed.
+5
Plynx
source
to share