JQuery Chrome - Prevent key entry

I have a tutorial-based webpage where users can go to the next page of the tutorial using the href arrow buttons (only if they perform some tasks). Also on most pages, the enter key will take you to the next page.

However, some training pages use the enter key for different purposes, and I want this key to be disabled. I have done the following:

$(window).keyup(function(e) {
    if (e.keyCode == 13)
    { 
        e.preventDefault();         
        return false;
    }
});

which works fine in IE / FF / Opera, but it is not in chrome.

In chrome, if I add a warning inside if, then the form will not be submitted. otherwise it will be.

Any idea?

+3
source share
2 answers
$(function () {

        $('form').bind("keypress", function (e) {
            if (e.keyCode == 13) return false;
        });


    });
+8
source

Try changing windowto "form".

Also consider adding e.stopPropagation();.

+1
source

All Articles