Disable keyboard in some fields

Asked

Viewed 1,506 times

5

I need to make the letter f only work in text fields, in other places it does not work, example:

$(document).keyup(function(e) {
    if (e.keyCode == 37) { 
               return false;
     }
});

1 answer

5


For this it is enough that, in the text fields, you prevent the propagation event - so that the generic code does not act on it. Also, it is better to deal with the keydown instead of the keyup because - the moment the keyup is activated - the effect of pressing the button has already happened.

$(document).keydown(function(e) {
    if (e.keyCode == 70) { 
        return false; // Equivalente a: e.stopPropagation(); e.preventDefault();
    }
});

$('input[type="text"]').keydown(function(e) {
    e.stopPropagation();
});

Example in jsFiddle. In the textarea key F will have no effect (as it is being treated by Handler global), but in the text box it works normally.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.