0

Lets say i attached an event handler to the document:

    $(document).on("keydown", function(e) {
        if(e.keyCode == 38){
           alert("Danger");
        }
    });

How can i check now in another function if this event handler is attached to the document. To be more clearer i would like to do something like this:

   if($(document).on("keydown"){
       $(document).off("keydown");
   }

Thanks!

John Smith
  • 6,105
  • 16
  • 58
  • 109
  • There is no harm in trying to remove a listener that is not attached. – Paul S. May 10 '14 at 12:02
  • Yes i know my example is not realistic, but if there is no attached keydown i would like to generate a new! – John Smith May 10 '14 at 12:04
  • 1
    You'll either have to use the **depreciated** `jQuery._data(element, 'events')` (don't modify this as you're accessing the inernals of jQuery), set your own flag when one is added or assume it's there, remove it and add a new one – Paul S. May 10 '14 at 12:07
  • If you goal is to just unbind event, then no need to check anything – A. Wolff May 10 '14 at 12:17

1 Answers1

0

you can use $._data, try this to get all events attached to document :

$._data($(document)[0], "events")

check this post for more

Community
  • 1
  • 1
Amir Sherafatian
  • 2,083
  • 2
  • 20
  • 32