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.
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(); });
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With