I’m building some hotkeys into a page of my HTML:
$(document).bind('keypress', function (e) {
var event = (e.keyCode ? e.keyCode : e.which);
if (event == LETTER_P) {
// go to another page
}
}
But the problem is that if they are entering text on the page (in a textbox element, etc) and hit the letter ‘P’, they are off to another page. I only want the redirection to happen if they are not in any elements.
document.activeElementis supported by all major browsers these days. If no element is in focus thenactiveElementwill return the document body, so: