Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to disable JQuery keypress event for just one element?

On my site I have registered a keypress event handler for the whole document.

$(document).keypress(myhandler);

And I handle the 'space' key to scroll a list.

Trouble is, there is an <input type='text' /> element, and I don't want 'space' keypress to scroll the list when it is entered in the input.

I couldn't find any information in the "event" object passed by JQuery to the handler, to identify where the source of the event is.

like image 632
HRJ Avatar asked Nov 10 '09 14:11

HRJ


People also ask

How do I turn off keypress events?

Alternatively, you could attach another event handler to the input field, and in this handler stop the propagation of the event: jQuery('#input-field-id'). bind('keypress', function(e) { e. stopPropagation(); });

What is the difference between keypress () and Keydown ()?

keydown – fires when any key is pressed down, fires first, and always before the browser processes the key (e.g. inserting text, moving focus, etc). keypress – fires when a key that produces a character value is pressed down, fires after keydown , and before the browser processes the key.

What is Keyup and Keydown in jQuery?

jQuery keyup() Method The order of events related to the keyup event: keydown - The key is on its way down. keypress - The key is pressed down. keyup - The key is released.

What is Keydown in jQuery?

The keydown event occurs when a keyboard key is pressed down. The keydown() method triggers the keydown event, or attaches a function to run when a keydown event occurs.


1 Answers

Alternatively, you could attach another event handler to the input field, and in this handler stop the propagation of the event:

jQuery('#input-field-id').bind('keypress', function(e) {
    e.stopPropagation(); 
});

This way, you can leave the global event handler as it is. Might be cleaner.

like image 122
Tom Bartel Avatar answered Oct 13 '22 07:10

Tom Bartel