Table of Contents

  1. JavaScript Mouse Events
  2. JavaScript Keyboard Events
  3. JavaScript Form Events

JavaScript Mouse Events


The examples seen below are powered by two functions. The first is attachEventHandler, which configures an event listener for an element with an ID elementId. It then binds that element with a handler that specifies what behavior should result from said event:

         
function attachEventHandler(elementId, event, handler) {
  const element = document.getElementById(elementId);
  if (element) {
    element.addEventListener(event, handler);
  } else {
    console.error(`Element with ID "${elementId}" not found.`);
  }
}
          

There are event handler functions for each of the unique mouse events in JavaScript, which make calls to updateDisplay. When a call is made, the function changes the content of a visual area in the page, using the metadata associated with the triggering event:

function updateDisplay(elementId, content) {
  const element = document.getElementById(elementId);
  if (element) {
    element.textContent = content;
  } else {
    console.error(`Display element "${elementId}" not found.`)
  }
}
        

All of the identifier strings for JavaScript's mouse events are listed below:

  1. click
  2. dblclick
  3. mouseover
  4. mouseout
  5. mouseenter
  6. mouseleave
  7. mousedown
  8. mouseup
  9. mousemove
  10. contextmenu
  11. mousewheel
  12. DOMMouseScroll

Click Example (click)

Click me
Click info will appear here

Double Click Example (dblclick)

Double click me
Click info will appear here

Mouse Over Example (mouseover)

Mouse over me
Click info will appear here

Mouse Out Example (mouseout)

Mouse out of me
Click info will appear here

Mouse Enter Example (mouseenter)

Mouse into me
Click info will appear here

Mouse Leave Example (mouseleave)

Make the mouse leave me
Click info will appear here

Mouse Down Example (mousedown)

Click mouse button down inside me
Click info will appear here

Mouse Up Example (mouseup)

Release clicked mouse button inside me
Click info will appear here

Mouse Movement Example (mousemove)

Move mouse here
Mouse coordinates will appear here

Context Menu Example (contextmenu)

Attempt to open a context menu inside me
Click info will appear here

Mouse Wheel Example (mousewheel)

Scroll the mouse wheel inside me
Click info will appear here

DOM Mouse Scroll Example (DOMMouseScroll)

Scroll inside me (Firefox legacy)
Scroll info will appear here

JavaScript Keyboard Events


Keyboard events are triggered when users interact with the keyboard. The main keyboard events include:

  1. keydown - Fired when any key is pressed down
  2. keyup - Fired when any key is released
  3. keypress - Fired when a character key is pressed (deprecated but still used)

Key Down Example (keydown)

Key info will appear here

Key Up Example (keyup)

Key info will appear here

Key Press Example (keypress)

Key info will appear here

JavaScript Form Events


Form events are triggered when users interact with form elements. The main form events include:

  1. submit - Fired when a form is submitted
  2. input - Fired when an input value changes
  3. change - Fired when an input loses focus and its value has changed
  4. focus - Fired when an element gains focus
  5. blur - Fired when an element loses focus
  6. reset - Fired when a form is reset

Form Submit Example (submit)

Form info will appear here

Input Event Example (input)

Input info will appear here

Change Event Example (change)

Change info will appear here

Focus Event Example (focus)

Focus info will appear here

Blur Event Example (blur)

Blur info will appear here Reset info will appear here