The advantage of a front end language like javascript (in fact, javascript is really the only browser-based language we have) is that you can respond immediately to a user’s actions. Javascript calls these “events”. An event could be a mouse move, a scroll, a key press, and many others. Here’s a list of the major Javascript events. Notice that not all of them are supported in all browsers.

Javascript won’t tell you automatically when events happen; you have to register an Event Listener to pay attention to that particular event — to “listen for” a mouse click, for example. When you create an event listener, you then define a function which will fire when the even happens.

element.addEventListener(event, function, useCapture);
  • The first parameter is the type of the event (like “click” or “mousedown”).
    Note that you don’t use the “on” prefix for the event; use “click” instead of “onclick”.
  • The second parameter is the function we want to call when the event occurs.
  • The third parameter is a boolean value specifying whether to use event bubbling or event capturing. The third parameter is optional.