I’ve bound some events to happen on left and right arrow key press like so:
$(document).keydown(function(e) {
switch(e.which) {
case 39: $("#next").trigger('click');
break;
case 37: $("#prev").trigger('click');
break;
}
});
However, obviously if you are in a form and press left and right to move through text, these events fire.
How do I change this so that that doesn’t happen?
You could check the target of the event (more information here)
or you could prevent the event from bubbling up by attaching an event handler to the input elements:
Update:
Likewise you might want to test the node name for
TEXTAREA.Here is an example: http://jsfiddle.net/86CKw/1/