2

I am trying to find an event listener I have attached to the document, but when I console.log it i get an error. I have tried to get do getElementById and getElementsByClassName, but i couldn't see the Event listener I attached to it. Here is a test code

document.addEventListener('keydown', test,false);

function test(e) {
     console.log(document);
}

What i want to do is grab the event listener save it and re-added later on in another class. Something

var savedListener = document._listener; 
document.removeAllEventListener();

and maybe in another class for example

document._listener = savedListener ;
Praveen Kumar Purushothaman
  • 164,888
  • 24
  • 203
  • 252
user629283
  • 329
  • 1
  • 8
  • 23
  • Possible duplicate of [Get event listeners attached to node using addEventListener](http://stackoverflow.com/questions/9046741/get-event-listeners-attached-to-node-using-addeventlistener) – Matt Jan 20 '16 at 11:46
  • @Matt-SL i tried that but it kept saying function undefined or i am just using it wrong. – user629283 Jan 20 '16 at 12:13

1 Answers1

0

You can store the different parameters of your event in a variable and use it later:

var evtToSave = {
  "event": 'keydown',
  "function": test,
  "useCapture": false
};

document.addEventListener(evtToSave.event, evtToSave.function, evtToSave.useCapture);

function test(e) {
  console.log(document);
}

//document.removeAllEventListener();
document.removeEventListener(evtToSave.event, evtToSave.function);


document.addEventListener(evtToSave.event, evtToSave.function, evtToSave.useCapture);
Magicprog.fr
  • 4,072
  • 4
  • 26
  • 35
  • That way is fine but if i have something 2 classes. I don't want the other class to know what event function it has. just needs to know save that event and add it later after executing your stuff. so if both class has the keypress event handler it doesnt need to know what it is. just need to know it has a listener save it and apply later. do you get what i mean? – user629283 Jan 20 '16 at 12:13