I have a live search input that shows results and lets you use the up and down arrows when the results popup but I want to prevent the cursor from moving left or right when using the up and down arrows. I cannot figure this out. I tried e.preventDefault() with no luck. Here is what I have tried:
if(e.which == 40){
e.preventDefault();
current = results.find('li.hover');
current.removeClass();
var next = current.next('li');
if(next.length == 0){
next = current.parent().parent().parent().next().find('li:first');
/* Go back to the top */
if(next.length == 0){
next = results.find('li:first');
}
}
next.addClass('hover');
return false;
}
Thanks!
Are you binding this onkeyup or keydown? If you are using keydown, you can prevent it, if your using keyup, the event already happened. Otherwise e.preventDefault() should be working.