DEV Community

Taiwo Shobo
Taiwo Shobo

Posted on

Simplified Guide to Browser Events

What are Browser Events?

A browser event is a signal that something has happened, such as a user clicking a button or typing in a form. Every DOM (Document Object Model) node can generate events, making them crucial for interactive web applications.


Common Types of Events

Mouse Events

  • click: Triggered when an element is clicked (or tapped on a touchscreen).
  • contextmenu: Fires on a right-click.
  • mouseover / mouseout: When the cursor moves over or leaves an element.
  • mousedown / mouseup: When a mouse button is pressed or released on an element.
  • mousemove: Fires when the mouse is moved.

Keyboard Events

  • keydown / keyup: Triggered when a key is pressed or released.

Form Events

  • submit: Fires when a form is submitted.
  • focus: Activates when an element, like an input field, gains focus.

Document Events

  • DOMContentLoaded: Happens when the HTML is fully loaded and the DOM is ready.

CSS Events

  • transitionend: Fires when a CSS animation finishes.

How to React to Events: Event Handlers

An event handler is a JavaScript function that runs when an event occurs. There are three common ways to assign event handlers:

1. HTML Attribute

Assign a handler directly in the HTML tag using an on<event> attribute.

<input type="button" value="Click me" onclick="alert('Button clicked!')" />
Enter fullscreen mode Exit fullscreen mode

2. DOM Property

Assign a handler using JavaScript.

<input id="button" type="button" value="Click me" />
<script>
  document.getElementById('button').onclick = function() {
    alert('Button clicked!');
  };
</script>
Enter fullscreen mode Exit fullscreen mode

3. addEventListener Method

This allows you to attach multiple handlers to the same event.

<input id="button" type="button" value="Click me" />
<script>
  const button = document.getElementById('button');
  button.addEventListener('click', () => alert('First handler!'));
  button.addEventListener('click', () => alert('Second handler!'));
</script>
Enter fullscreen mode Exit fullscreen mode

Working with the Event Object

When an event occurs, the browser creates an event object containing details about the event. For example:

<input id="button" type="button" value="Click me" />
<script>
  document.getElementById('button').onclick = function(event) {
    alert(`Clicked at X:${event.clientX}, Y:${event.clientY}`);
  };
</script>
Enter fullscreen mode Exit fullscreen mode

Common Properties in the Event Object:

  • event.type: The type of event (e.g., "click").
  • event.currentTarget: The element that triggered the event.
  • event.clientX / event.clientY: Coordinates of the mouse pointer relative to the window.

When to Use addEventListener

The addEventListener method is more flexible because:

  1. It allows multiple handlers for the same event.
  2. It supports additional options like once (run the handler only once) and passive (prevent blocking default actions).
  3. Some events, like DOMContentLoaded, only work with addEventListener.

Summary

  1. HTML attributes are easy but clutter HTML.
  2. DOM properties work but allow only one handler per event.
  3. addEventListener is the most versatile method for handling events.

In most cases, addEventListener is the preferred choice. Keep exploring more event types and their usage to make your web applications more interactive!

Top comments (0)