Example of working code

The javascript here successfully adds an event listener to the button.

by liffiton

HTML

<html>
  <head>
    <script type="text/javascript">
      // Our example event handler
      function handleClick(event) {
    	  alert("Clicked!");
			};
      
     	// A function for attaching events to DOM elements
			function setupEvents() {
      	const elem = document.querySelector("#myelement");
        elem.addEventListener('click', handleClick);
      };
  
			// Attach our setup function to DOMContentLoaded
 			document.addEventListener('DOMContentLoaded', setupEvents);
  	</script>
  </head>
  
  <body>
    <button id="myelement">
      Click Me
    </button>
  </body>
</html>