5

I try to find a way to check if an eventListener already exists in a window, I've tried the solution in this similar question and more other solutions, but nothing works for me.

Here is the code:

   checkEvent=function()
   {
    if('keydown' in window)
        console.log('exist');
    else
        console.log('not_exist');
   }

And here is my eventListener that I add on my page Load

document.addEventListener("keydown", PayKeydownHandler);

But the first code, returns 'not_exist' as a result

PS: The function checkEvent(), i call it in onclick of ENTER keydown.

Any help please? Thank's in advance.

Merji
  • 69
  • 1
  • 1
  • 7
  • First of all, note that your function looks at `window`, but you add the event listener to `document`. Second, if you're only going to add one listener, you could probably just directly set the `onkeydown` property and then have `checkEvent` check if that property is set to a function. Otherwise, I don't believe there's a built in way to do this. – pushkin Dec 06 '17 at 17:58
  • See the answer from: https://stackoverflow.com/questions/11430672/javascript-how-to-check-if-event-already-added – CoursesWeb Dec 06 '17 at 18:01
  • @pushkin you're right, I didn't even pay attention that I'm working with document while, I'm testing on window. But even with that I have the same result. Thank's for your reply – Merji Dec 07 '17 at 10:19
  • @CoursesWeb I have tried this solution too, but the console showed me an error in the syntax. Thank's for your reply – Merji Dec 07 '17 at 10:21

1 Answers1

-3

try this

jQuery._data( window, "events" );

it work in jquery 1.8 . Read this jQuery blog post.

ali zarei
  • 1,212
  • 11
  • 17