Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery keydown but ignore if in text box

I'm calling a function (below) that will perform an action if the user presses the delete button. It is working fine but I need to to only do it on the page and not when a user is typing (inside an input or inside a textarea).

$(window).keydown(function (evt) {
    if (evt.which == 46) { // delete
        goDoSomething();
    }
});

Any ideas how I can amend the above to not fire if the user is in an input or textarea?

Thanks in advance,

Dave

like image 960
dhardy Avatar asked Feb 28 '13 11:02

dhardy


1 Answers

check evt.target's type:

$(window).keydown(function (evt) {
    if (evt.target.tagName.toLowerCase() !== 'input' &&
        evt.target.tagName.toLowerCase() !== 'textarea' && evt.which == 46) { // delete
        goDoSomething();
    }
});
like image 175
karaxuna Avatar answered Nov 03 '22 04:11

karaxuna