window.##EventListener not working when returned from a function

I did in fact look for this in the search bar so don't even try to mark this one as "already answered."

So in my code I have multiple functions that use window.add(remove)EventListener(...). I found this trick from a youtube channel where the guy used a function to get document.getElementById instead of writing a bunch of times. But its not working, I tried something like this:

function addListen(x, y) {
   return window.addEventListener(x, y);
}

Then maybe call it like this:

...addListen("scroll", funcName)..

To be specific I was calling it from a ternary operator: i == # ? addListen(x, y):...

Answers


My simple example does work (http://jsfiddle.net/NameFILIP/ohxjarnq/2/):

function addListen(x, y) {
  return window.addEventListener(x, y);
}
function doNothing() {}
var i = 3;
i === 3 ? addListen("click", function () { 
  alert('clicked');   
}) : doNothing();

Maybe something went wrong with your ternary operator? Check has the addListen function been executed? Take into account that it's recommended to always use ===.

You can also use a Visual Event trick to inspect your event listeners.


Need Your Help

Force validation on bound controls in WPF

wpf validation business-objects

I have a WPF dialog with a couple of textboxes on it.

reset() - “Strict Standards: Only variables should be passed by reference”

php reference strict

I am currently moving a website over for a client. Their old host must be using an old version of PHP from what I can gather. The site is currently working on their current host. I am working to move