Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery: do this on keyup, unless the person is in a textarea or input

Tags:

jquery

onkeyup

my script works but i don't understand how to make it NOT launch the functions when in a textarea/input and those keys are pressed. aka: launch the event when the user presses that key, unless the user is in a textarea/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;    
}
     })
like image 292
android.nick Avatar asked Sep 24 '10 14:09

android.nick


3 Answers

Just check event.target:

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

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

like image 108
fantactuka Avatar answered Nov 15 '22 09:11

fantactuka


Try:

$('body *:not(textarea,input)').keyup(function (event) {

});
like image 32
Thariama Avatar answered Nov 15 '22 10:11

Thariama


$('body :not(textarea,input[type=text])').keyup(function (event) {

or

$('body').not('textarea,input[type=text]').keyup(function (event) {
like image 25
Dave Thieben Avatar answered Nov 15 '22 08:11

Dave Thieben