tags:

views:

41

answers:

3

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;    
}
     })
+1  A: 

Try:

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

});
Thariama
Such selection is incorrect since it will add as much event handlers as much elements you have on the page.
fantactuka
almost right (textareas are not included), but this will work
Thariama
+1  A: 
$('body :not(textarea,input[type=text])').keyup(function (event) {

or

$('body').not('textarea,input[type=text]').keyup(function (event) {
dave thieben
This one has same problem as Thariama's solution
fantactuka
that's what he asked for.
dave thieben
Just imagine that page has 10000 elements (not too much). Will you assign event to each element? It does not makes sense
fantactuka
I suppose it's a good design decision for the OP. however, not what he was asking about.
dave thieben
I'm just suggesting to think about performance of your solution. Whatever he asked for.
fantactuka
+2  A: 

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
Why assign the event to the input in the first place if you never want to handle it?
GenericTypeTea
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
+1 efficiently done
Thariama