Question

I have a form on which user can tab and jump to different elements. i was to stop the tab when it reaches a special anchor tag.

this is the code which work in firefox

    $('.next-tab').keypress(function(e){
        var code = (e.keyCode ? e.keyCode : e.which);
        console.log(code);
        if (code == 0 || code == 9){
            console.log("keypress")
            e.preventDefault();
            e.stopPropagation();
        }
    });

but this code does not work in chrome, i dont know why, it does not even enter the keypress method. so i used this code for chrome

$('.next-tab').blur(function(e) {
            var code = (e.keyCode ? e.keyCode : e.which);
            console.log(code);
            if (code == 0 || code == 9){
                console.log("blur")
                e.preventDefault();
                e.stopPropagation();
            }
        });

it enters the blur method pass the condition but dont do any thing, and user can easily move to the next element.

Était-ce utile?

La solution

For chrome support, looks like you need to use keydown event:

http://jsfiddle.net/qD2rk/

$('.next-tab').keydown(function (e) {
    var code = (e.keyCode ? e.keyCode : e.which);
    console.log(code);
    if (code == 0 || code == 9) {
        console.log("keydown")
        e.preventDefault();
        e.stopPropagation();
    }
});
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top