We can not only assign handlers, but also generate events from JavaScript.
Custom events can be used to create “graphical components”. For instance, a root element of our own JS-based menu may trigger events telling what happens with the menu: open (menu open), select (an item is selected) and so on. Another code may listen for the events and observe what’s happening with the menu.
in html file
<button id="elem" myClick="alert('Click!');">Autoclick</button>
in js file
<script>
let event = new Event("click"); elem.dispatchEvent(event);
</script>