I have this code, I will need to programmatically overridde or remove onkeydown from the document (for example, using a simple condition)
document.onkeydown = function (f) { myMethod(); };
Any idea how to do this?
document.onkeydown = null
You can use jquery to handle your events for you, the method you use is less commonly used and does not allow multiple subscribers.
Check out the documentation here:
http://api.jquery.com/on/
Example:
$(document).on("keydown", function(){ doStuff(); }) // unsubscribe all handlers $(document).off("keydown");
You can remove it with:
document.onkeydown = null;
If you want to restore it, you can first save it in a variable:
var saved_keydown = document.onkeydown; document.onkeydown = null; //... later document.onkeydown = saved_keydown;
You cannot delete it, you can only attach a new one that does nothing, for example:
document.onkeydown = function () {};
using jQuery you can do:
$(document).on('keydown', myMethod);
and delete
$(document).off('keydown', myMethod);
The easiest way is to set to null
Source: https://habr.com/ru/post/949540/More articles:What xcode project files should be added to the version control system - version-controlC ++ built-in scripting language for game development - can't find anything I like - c ++How to enable custom configuration for another environment in Laravel-4 - laravelDerived Generic Types - javaInstall PMD 5.0.4 in eclipse - javaJava error: possible loss of accuracy - javaHow to change the color of focused EditText when using "android: Theme.Holo.Light"? - androidInvalid Android batch file - androidHow to parse this nested JSON array in android - jsonCheck and read the internal json array that is in the main json array in android? - javaAll Articles