Q:

addEventListener without handler

I want to set an Event on an Element without inline Events like:

    let myDiv = document.querySelector("#div");
  myDiv.addEventListener("click",function({this.style.opacity = "0";})
<div onclick="zero()"></div>
    this is code:
    
    <div id="div"></div>

I expected the opacity of div Element changes to 0 but in console of the browser show this error:
Uncaught error: cannot read property addEventListener of null!

A:

You're missing some brackets and introducing an unnecessary line break:

let myDiv = document.querySelector("#div");

myDiv.addEventListener("click", function() {
  this.style.opacity = "0";
});
<div id="div">Hallo</div>

With regard to the error you're getting it suggests that you haven't allowed the DOM to load - ie you're trying to add an event listener to an element that isn't there. Your code is in the right order in your question but it's something to be mindful of.

