Jquery: do it on the keyboard, unless the person is in a text box or input field

my script works, but I don’t understand how to make it NOT run functions when these keys are pressed in textarea / input. aka: trigger an event when the user presses this key if the user is not in the text box / input.

$('body').keyup(function (event) { var direction = null; if (event.keyCode == 37) { $('#wrapper').fadeOut(500) } else if (event.keyCode == 39) { $('html,body').animate({scrollTop: $('body').offset().top}, {duration: 1500, easing: 'easeInOutQuart'} ) return false; } }) 
+6
jquery onkeyup
source share
3 answers

Just check event.target:

 $('body').keyup(function(event) { if ($(event.target).is(':not(input, textarea)')) { ... } }); 

In this case, you will have only one event handler (attached to the body), but it will filter the elements that receive the event

+10
source share

Try:

 $('body *:not(textarea,input)').keyup(function (event) { }); 
+1
source share
 $('body :not(textarea,input[type=text])').keyup(function (event) { 

or

 $('body').not('textarea,input[type=text]').keyup(function (event) { 
+1
source share

All Articles