Skip to content Skip to sidebar Skip to footer

Excluding Form Fields From Keypress Handler Assigned To Body

I have a keypress handler on a web page assigned to the body element. I really do want it to be active anywhere in the web page. Or so I thought. The keypress events in textual

Solution 1:

It would be easier simply to check which element triggered the event in your keypress handler and filter out input elements:

document.onkeypress = function(evt) {
    evt = evt || window.event;
    var target = evt.target || evt.srcElement;
    if ( !/INPUT|TEXTAREA|SELECT|BUTTON/.test(target.nodeName) ) {
        // Do stuff
    }
};

Post a Comment for "Excluding Form Fields From Keypress Handler Assigned To Body"