Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove an eventhandler document.onkeydown?

I have this code, I would need programmatically overridde or remove onkeydown from document (for example using a simple condition)

document.onkeydown = function (f) {
    myMethod();
};

Any idea how to do it?

like image 875
GibboK Avatar asked Jul 16 '13 09:07

GibboK


People also ask

What does Onkeydown mean?

Definition and Usage The onkeydown attribute fires when the user is pressing a key (on the keyboard).

What is the Keydown event?

The keydown event is fired when a key is pressed. Unlike the deprecated keypress event, the keydown event is fired for all keys, regardless of whether they produce a character value. The keydown and keyup events provide a code indicating which key is pressed, while keypress indicates which character was entered.


1 Answers

document.onkeydown = null

You could use jquery to do your event handeling for you, the method you use is less commonly used and doesn't allow multiple subscribers.

Look at the documentation here:

http://api.jquery.com/on/

example:

$(document).on("keydown", function(){
    doStuff();
})
// unsubscribe all handlers
$(document).off("keydown");
like image 178
Willem D'Haeseleer Avatar answered Sep 26 '22 22:09

Willem D'Haeseleer