Possible Duplicate:
Javascript event handler on body but not on input
I wrote a short script that listens the keydown event, but i’d like to ignore it if i’m writing in a text field.
I have no idea about how to do it without strange tricks, like checking if there is a focus on one of the inputs.
Here is my code right now.
document.addEventListener('keydown', function(event) {
if(event.keyCode == 71) {
showSelected();
}
else if(event.keyCode == 13) {
closeModal();
}
});
This is a little tricky if you’re doing things with
contenteditable(like letting users write text inside of any divs/spans/boxes they want to on the page).If so, this will take a little thinking and reworking (it’s not that hard – just more involved).
However, you can check the
event.target(element the event’s happening on) for itstagNameproperty, against a list of types you want to exclude.Should also likely be noted that this solution (and your code thus-far) aren’t ghetto-IE compatible.
For that, you’d need to work with
attachEventand thewindow.eventandwindow.event.srcElementproperties, because they don’t supportaddEventListenerand thee/eventfunction-parameter.