Unregister from JQUERY Keydown event

I am trying to remove the keydown event that I created using the following code:

$(document).keydown(function(e) { alert("Key Down"); }); 

when i use the following code:

 $(document).off('keydown'); 

it produces the following error:

 Uncaught TypeError: Object [object Object] has no method 'off' 

can someone tell me the correct way to unregister from keydown event?

+4
source share
1 answer

Untie all keydown handlers from document :

 $(document).unbind('keydown'); 

Or to make sure that you only untie this particular handler:

 function myHandler(e) { alert("Key Down"); } $(document).keydown(myHandler); // later $(document).unbind('keydown', myHandler); 

http://api.jquery.com/unbind

+13
source

Source: https://habr.com/ru/post/1443967/


All Articles