Skip to the content.
events

Events

Events are fired to notify code of "interesting changes" that may affect code execution. These can arise from user interactions such as using a mouse or resizing a window, changes in the state of the underlying environment (e.g. low battery or media events from the operating system), and other causes.

Here’s a simple example of handling a click event:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>JavaScript Event Example</title>
  </head>
  <body>
    <button id="myButton">Click me</button>

    <script>
      // Get a reference to the button element
      const button = document.getElementById("myButton");

      // Define a function to be called when the button is clicked
      function handleClick() {
        alert("Button clicked!");
      }

      // Attach the event listener to the button
      button.addEventListener("click", handleClick);
    </script>
  </body>
</html>

Types of HTML Element Events

HTML elements can respond to a wide variety of events, allowing developers to create interactive and dynamic web pages. These events can be broadly categorized based on their purpose and the user actions or system changes that trigger them.

Common Categories of HTML Element Events

Event Category Description Example Events
Mouse Events Triggered by mouse actions. onclick, onmouseover, onmouseout, onmousedown, onmouseup, ondblclick[1][2][5]
Keyboard Events Triggered by keyboard actions. onkeydown, onkeyup, onkeypress[2][5]
Form Events Related to form input and submission. onsubmit, onchange, oninput, onfocus, onblur, onreset[2][5][6]
Drag & Drop Events Involved in drag-and-drop operations. ondrag, ondragstart, ondragend, ondragenter, ondragleave, ondragover, ondrop[1][2]
Clipboard Events Triggered by cut, copy, and paste actions. oncopy, oncut, onpaste[2]
Media Events Related to media elements like audio and video. onplay, onpause, onended, onvolumechange, ontimeupdate[1][2]
Focus Events Occur when elements gain or lose focus. onfocus, onblur[2][5]
Load/Unload Events Related to the loading and unloading of documents or resources. onload, onunload, onbeforeunload[5][6]
Touch Events Triggered by touch interactions (mainly on mobile devices). ontouchstart, ontouchend, ontouchmove, ontouchcancel[2]
Wheel/Scroll Events Triggered by scrolling or mouse wheel actions. onscroll, onwheel[1][2]
Animation/Transition Related to CSS animations and transitions. onanimationstart, onanimationend, ontransitionend[2]
Mutation Events Triggered when the DOM structure changes (now largely replaced by MutationObserver API). DOMSubtreeModified, DOMNodeInserted, DOMNodeRemoved[2]
Input Events Triggered by changes to input fields. oninput, onchange[5]

Examples of Event Usage

How to Attach Event Handlers

Comprehensive References

These sources provide exhaustive lists and documentation for all standard HTML events.

Framework transformation of events

HTML events are handled differently in modern frameworks and libraries to provide enhanced developer experience, maintainability, and integration with their component models. Here’s how each major technology transforms and manages HTML element events:


React


Angular


Vue


Web Components


Comparison Table

Framework/Library Event Syntax Example Handler Binding Event Object Modifiers/Features
React <button onClick={fn}> Function in JSX SyntheticEvent Arrow functions for args, preventDefault, stopPropagation
Angular <button (click)="fn()"> Method in component $event (native) Key/code modifiers, template expressions
Vue <button @click="fn"> Method or inline Native event .stop, .prevent, .once, key modifiers
Web Components addEventListener('click', fn) Direct DOM binding Native event Custom events, event delegation, cleanup in lifecycle

Summary

HTML elements support a rich set of event types, including mouse, keyboard, form, media, drag-and-drop, clipboard, and more. Developers can attach event handlers using inline attributes, DOM properties, or the addEventListener method for robust and interactive web applications[1][2][3][5][6].

Each approach reflects the framework’s philosophy: React and Vue integrate events tightly with their component models, Angular provides declarative and expressive syntax with modifiers, and Web Components rely on native browser APIs for maximum flexibility and interoperability.

References