I have a webpage that has lots of 'windows', which aren't wrapped in iframes or anything silly. Each 'window' is its own 'App'.
One of these Apps has a ton DOM elements in it. I want to bind a listener to that App, which is encased in a DIV, and want to capture some hotkey presses such as the left and right arrows.
However, if some element that actually rightfully deserves focus (such as an input inside of this app) gets a left or right keypress, I want to ignore it.
<div class='app-wrapper' tabindex='1'><!-- behold ye content --></div>
$('.app-wrapper').on('keypress', function(){
// ... I dont think this will work.
});
Is there any good solution to achieving the above without a major hack job?
You could look at e.target
in your listener function:
$('.app-wrapper').on('keypress', function(e){
if(e.target.tagName === 'DIV'){ //Or, more jQuery-esque: $(e.target).is('div')
console.log("Wooo, I'm totally a div. Let's do stuff");
}
});
In that way you can ignore keypresses fired inside "valid" elements.