Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unregistering from the JQUERY Keydown event

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

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

when I am using the following code:

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

it throws the following error:

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

can someone point me to the correct way to un-register from the keydown event?

like image 566
user1322801 Avatar asked Dec 11 '22 21:12

user1322801


1 Answers

Unbind all keydown handlers from document:

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

Or to make sure you only unbind that specific handler:

function myHandler(e) {
    alert("Key Down");
}

$(document).keydown(myHandler);
// later
$(document).unbind('keydown', myHandler);

http://api.jquery.com/unbind

like image 109
Matt Ball Avatar answered Dec 28 '22 03:12

Matt Ball