ES6 and addeventlistener(). Why does this method not accept reference from an anonymous function?

Asked

Viewed 58 times

2

Why click is not invoked?

$elemento.addEventListener('click', click, true);
var click = () => { ... };

1 answer

4


You must declare the variable with its function before trying to use it...

document.addEventListener('DOMContentLoaded', function() {
  var click = () => {
    alert("Clicou");
  };
  document.getElementById("test").addEventListener("click", click, true);
}, false);
<input type="button" value="test" id="test">

  • I get it. However, I thought that when declaring the variable that stores the anonymous function with var, it would be of global scope being accessed from any part of code even though it was declared before use. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.