JavaScript Events
Events are actions or occurrences that happen in the system you are programming, which the system tells you about so you can respond to them in some way if desired. For example, if the user clicks a button on a webpage, you might want to respond to that action by displaying an alert box.
addEventListener()
The modern and most robust way to handle events is using theaddEventListener()method. It allows you to attach an event handler to a specified element without overwriting existing event handlers.
The Event Object (e)
When an event occurs, an Event Object is automatically passed to the callback function. It contains rich information about the event that just happened. E.g.,e.target returns the exact element that triggered the event, and e.key tells you which key was pressed in a keyboard event.
preventDefault()
Some HTML elements have default behaviors (like forms submitting to a new page, or links navigating away). You can stop these actions to handle the logic entirely in JS by callinge.preventDefault() inside your event listener callback.
View Full Transcript+
This section contains the full detailed transcript. It covers why we prefer addEventListener over inline HTML onclick handlers (separation of concerns), how to remove event listeners using removeEventListener, and advanced concepts like event bubbling and capturing (propagation).
