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

Embedding Cython in C++

python c++ cython

I am trying to embed a piece of Cython code in a C++ project, such that I can compile a binary that has no dependencies on Python 2.7 (so users can run the executable without having Python installe...

instance variable is becoming null

java debugging

I am working on SWT based GUI application. I have one object variable which is private.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.