ES6 and addEventListener (). Why does not this method take reference to an anonymous function?

2

Why is the 'click' not invoked?

$elemento.addEventListener('click', click, true);
var click = () => { ... };
    
asked by anonymous 02.02.2017 / 13:51

1 answer

4

You must declare the variable with your function before attempting 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">
    
02.02.2017 / 14:01