Naota
Naota

Reputation: 668

Excluding form elements on keypress

I have a keypress assigned to a div, for example pressing A will show the div, press again to hide, problem is this also happens in form elements, have looked at other questions and answers but none worked for me.

I'm using:

$(document).keypress(function(ev) {
if (ev.which === 65 || ev.which === 97) { // 'A' or 'a'
     $('#mainMenu').toggle();
}
});

How can I exclude textfields from this?

Upvotes: 2

Views: 201

Answers (1)

roberkules
roberkules

Reputation: 6605

check the ev.target

something like:

if ($(ev.target).is(":input")) {
    return;
}

Upvotes: 4

Related Questions