6

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;    
}
     })
android.nick
  • 11,069
  • 23
  • 77
  • 112

3 Answers3

10

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

fantactuka
  • 3,298
  • 19
  • 29
  • Why assign the event to the input in the first place if you never want to handle it? – djdd87 Sep 24 '10 at 14:34
  • 1
    Here we assign event to the body, not to the input. And later just filter with input and textarea.. It's much better then assign event to all elements except inputs – fantactuka Sep 24 '10 at 14:45
1

Try:

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

});
Thariama
  • 50,002
  • 13
  • 138
  • 166
1
$('body :not(textarea,input[type=text])').keyup(function (event) {

or

$('body').not('textarea,input[type=text]').keyup(function (event) {
Dave Thieben
  • 5,388
  • 2
  • 28
  • 38