<body>
<div>Sample div</div>
<input type='text'/>
</body>
$(document).delegate('body *:not(input)', 'keyup', function(e){
if((e.keyCode || e.which) == 39){ //right arrow
$('#next_image').click();
}
if((e.keyCode || e.which) == 37){ //left arrow
$('#prev_image').click();
}
}
I’m using jQuery Cycle library to setup slideshow. It’s more like a photo album, where you can use keyboard shortcuts to do some stuff. So, the slideshow is the whole page, which also contains input fields. Hence, when you are writing a text inside of input field and try to move cursor with keyboard (left and right arrow buttons), it would fire the event and scroll to the next/prev image.
Any ideas on how to enable keyup events on everything BUT the input field? Obviously, current selector doesn’t work.
If you want to stick with
.delegate()instead of the currently-favored.on():event.whichIf you did have to check
keyCodeandwhich,(e.keyCode || e.which) == 39would not work properly, since||will evaluate to the first truthy operand, or the second operand if both are falsy. To put it differently: you’d have to check each one separately, like this:===instead of==. See Which equals operator (== vs ===) should be used in JavaScript comparisons?Ah yes. I’m thinking it’s because of event bubbling.
I’ve seen it solved using an extra event handler (imgur caption boxes in the gallery):
http://jsfiddle.net/mattball/DQusL/