JavaScript 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