Stops moving the cursor when using the up and down arrows

I have a direct search that shows the results and allows you to use the up and down arrows when the results pop up, but I want the cursor to not move left or right when using the up and down arrows. I can’t figure it out. I tried e.preventDefault()with no luck. Here is what I 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;
    }

Thank!

+5
source share
1 answer

Are you binding this onkeyup or keydown? If you use keydown, you can prevent it, if you use keyup, an event has already occurred. Otherwise, e.preventDefault () should work.

+11
source

All Articles