addEventListener Events List

The full list of event types thats valid for addEventListener()

Jul 5, 2023 | Read time 9 minutes

🔔 Table of contents

Introduction

If you have been using addEventListener to attach events to your DOM elements, you might wonder what are the type of events thats valid?

In this post I will go through all of the event types and the descriptions and targets that they accept!

The addEventListener method, a powerful tool that enables developers to listen for and respond to a wide range of events triggered by user interactions, system events, or even custom triggers.

The addEventListener syntax

If you have been following tutorials online, you might see some common event types :

  • Click events - “click” this captures user clicks - commonly with mouse or touch devices
  • Key events - keyboard inputs for enhanced user experiences.
  • Mouse events - Tracking mouse movements, clicks, and scrolls.
  • Form events - form submissions, input changes, and validations.
  • Touch events - touch events for mobile devices.

The specifications state that the function will only work for elements that support events.

The syntax will look like this:

addEventListener(type, listener, options)

  • type is a string of a “event type” and case-sensitive. So this means that ‘Click’ (and will not work) is different to ‘click’. You can see all possible event types here: Events
  • listener is a function reference.

A common way to use “addEventListener” is as follows:

    let button = document.querySelector('#myButton');
    button.addEventListener('click', function() {
        console.log('Button clicked!');
    });

What can trip you up is that the “listener” parameter has to be a function REFERENCE - and not function CALL

List of all event types

Here is the list of all event types that you can pass into the “addEventListener” function.

Keep in mind that the event type as to be case-sensitive!

Event Interface Targets Description
DOMContentLoaded Event Document Gets triggered at the Document object. This happens when everything has been loaded and parsed!
afterprint Event Window This gets triggered at the Window object after the user prints
beforeprint Event Window Gets triggered at the Window object before printing
beforematch Event Elements Gets triggered on elements with the hidden=until-found attribute before they are revealed.
beforetoggle ToggleEvent Elements Gets triggered on elements with the popover attribute when they are transitioning between showing and hidden
beforeunload BeforeUnloadEvent Window Gets triggered at the Window object when the page is about to be unloaded. This is common for use when we want to show a prompt before the user leaves the page - eg “Are you sure you want to leave?”
blur Event Window, elements Gets triggered at DOM lements when they stop being in focus. For example when the user moves away from the <input>
cancel Event dialog elements, input elements Gets triggered at dialog elements when they are canceled by the user (e.g., by pressing the Escape key), or at input elements in the File state when the user does not change their selection
change Event Form controls Gets triggered at controls when the user commits a value change (see also the input event)
click PointerEvent Elements Normally a mouse event; also synthetically Gets triggered at an element before its activation behavior is run, when an element is activated from a non-pointer input device (e.g. a keyboard)
close Event dialog elements Gets triggered at dialog elements when they are closed
connect MessageEvent SharedWorkerGlobalScope Gets triggered at a shared worker’s global scope when a new client connects
contextlost Event canvas elements, OffscreenCanvas objects Gets triggered when the corresponding CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D is lost
contextrestored Event canvas elements, OffscreenCanvas objects Gets triggered when the corresponding CanvasRenderingContext2D or OffscreenCanvasRenderingContext2D is restored after being lost
currententrychange NavigationCurrentEntryChangeEvent Navigation Gets triggered when navigation.currentEntry changes
dispose Event NavigationHistoryEntry Gets triggered when the session history entry corresponding to the NavigationHistoryEntry has been permanently evicted from session history and can no longer be traversed to
error Event or ErrorEvent Global scope objects, Worker objects, elements, networking-related objects Gets triggered when unexpected errors occur (e.g. networking errors, script errors, decoding errors)
focus Event Window, elements Gets triggered at nodes gaining focus
formdata FormDataEvent form elements Gets triggered at a form element when it is constructing the entry list
hashchange HashChangeEvent Window Gets triggered at the Window when the fragment part of the document’s URL changes
input Event Elements Gets triggered when the user changes the contenteditable element’s content, or the form control’s value. See also the change event for form controls.
invalid Event Form controls Gets triggered at controls during form validation if they do not satisfy their constraints
languagechange Event Global scope objects Gets triggered at the global scope object when the user’s preferred languages change
load Event Window, elements Gets triggered at the Window when the document has finished loading; Gets triggered at an element containing a resource (e.g. img, embed) when its resource has finished loading
message MessageEvent Window, EventSource, MessagePort, BroadcastChannel, DedicatedWorkerGlobalScope, Worker, ServiceWorkerContainer Gets triggered at an object when it receives a message
messageerror MessageEvent Window, MessagePort, BroadcastChannel, DedicatedWorkerGlobalScope, Worker, ServiceWorkerContainer Gets triggered at an object when it receives a message that cannot be deserialized
navigate NavigateEvent Navigation Gets triggered before the navigable navigates, reloads, traverses, or otherwise changes its URL
navigateerror ErrorEvent Navigation Gets triggered when a navigation does not complete successfully
navigatesuccess Event Navigation Gets triggered when a navigation completes successfully
offline Event Global scope objects Gets triggered at the global scope object when the network connections fails
online Event Global scope objects Gets triggered at the global scope object when the network connections returns
open Event EventSource Gets triggered at EventSource objects when a connection is established
pagehide PageTransitionEvent Window Gets triggered at the Window when the page’s session history entry stops being the active entry
pageshow PageTransitionEvent Window Gets triggered at the Window when the page’s session history entry becomes the active entry
pointercancel PointerEvent Elements and Text nodes Gets triggered at the source node when the user attempts to initiate a drag-and-drop operation
popstate PopStateEvent Window Gets triggered at the Window when in some cases of session history traversal
readystatechange Event Document Gets triggered at the Document when it finishes parsing and again when all its subresources have finished loading
rejectionhandled PromiseRejectionEvent Global scope objects Gets triggered at global scope objects when a previously-unhandled promise rejection becomes handled
reset Event form elements Gets triggered at a form element when it is reset
select Event Form controls Gets triggered at form controls when their text selection is adjusted. This can be from a API call or from the user
storage StorageEvent Window Gets triggered at Window event when the corresponding localStorage or sessionStorage storage areas change
submit SubmitEvent form elements Gets triggered at a form element when it is submitted
toggle Event or ToggleEvent details and popover elements Gets triggered at details elements when they open or close; Gets triggered on elements with the popover attribute when they are transitioning between showing and hidden
unhandledrejection PromiseRejectionEvent Global scope objects Gets triggered at global scope objects when a promise rejection goes unhandled
unload Event Window Gets triggered at the Window object when the page is being unloaded
visibilitychange Event Document Gets triggered at the Document object when the page becomes visible or hidden to the user

References

👋 About the Author

G'day! I am Huy a software engineer based in Australia. I have been creating design-centered software for the last 10 years both professionally and as a passion.

My aim to share what I have learnt with you! (and to help me remember 😅)

Follow along on Twitter , GitHub and YouTube