Back to course

Introduction to Event Handling

JavaScript: The Complete '0 to Hero' Beginner Course

88. Introduction to Event Handling

Web pages are dynamic because JavaScript can respond to events—actions that happen on the web page, usually initiated by the user or the browser.

What is an Event?

An event signals that something has happened. Common events include:

  • Mouse Events: click, mouseover, mousedown, mousemove
  • Keyboard Events: keydown, keyup
  • Form Events: submit, change, focus
  • Document Events: load, scroll

Event Handler vs. Event Listener

  1. Event Handler (Inline/Property): Directly setting a property on the element or using HTML attributes (e.g., <button onclick="myFunction()">). This is generally discouraged due to poor separation of concerns.
  2. Event Listener (Preferred): Using the addEventListener() method to attach a function (the handler) that runs when the event occurs. This allows you to attach multiple functions to the same event on the same element.