Events in JavaScript

Jason Wandrag
4 min readDec 28, 2022

Web Wizard’s weave events into their projects to give their applications different kinds of cool functionality

In JavaScript, events are actions or occurrences that happen in the browser, such as a user clicking a button, a page loading, or a form being submitted. Events are an important part of the JavaScript programming model, as they allow you to create interactive webpages that respond to user input.

To handle events in JavaScript, you can use event listeners, which are functions that are registered to be called in response to a specific event. Event listeners are added to an element using the addEventListener method, which takes two arguments: the name of the event to listen for, and the event handler function to be called when the event occurs.

Here’s an example of how you might use an event listener to handle a button click event:

const button = document.querySelector("button");
button.addEventListener("click", () => {
console.log("Button clicked!");
});

In this example, the event handler function is a simple anonymous function that logs a message to the console when the button is clicked. You can also use a named function as the event handler, like this:

function handleButtonClick() {
console.log("Button clicked!");
}

button.addEventListener("click", handleButtonClick);

There are many different types of events that you can handle in JavaScript, including mouse events (such as click, mouseover, and mouseout), keyboard events (such as keydown and keyup), and form events (such as submit and focus). You can find a complete list of DOM events in the MDN documentation.

There are many different types of events that can be handled in JavaScript, including:

Mouse events: These events are triggered by user interactions with the mouse, such as clicking, double-clicking, hovering, and dragging. Some common mouse events include:

  • click: Triggered when a mouse button is clicked
  • dblclick: Triggered when a mouse button is double-clicked
  • mousedown: Triggered when a mouse button is pressed down
  • mouseup: Triggered when a mouse button is released
  • mouseover: Triggered when the mouse cursor moves over an element
  • mouseout: Triggered when the mouse cursor moves out of an element
  • mousemove: Triggered when the mouse is moved
  • dragstart: Triggered when the user starts dragging an element
  • drag: Triggered when the user is dragging an element
  • dragend: Triggered when the user stops dragging an element

Keyboard events: These events are triggered by user interactions with the keyboard, such as pressing and releasing keys. Some common keyboard events include:

  • keydown: Triggered when a key is pressed down
  • keyup: Triggered when a key is released
  • keypress: Triggered when a character key is pressed

Form events: These events are triggered by user interactions with form elements, such as input fields, buttons, and select boxes. Some common form events include:

  • submit: Triggered when a form is submitted
  • focus: Triggered when an element receives focus
  • blur: Triggered when an element loses focus
  • change: Triggered when the value of an element is changed

Document events: These events are triggered by actions that affect the entire document, such as loading, unloading, or resizing the window. Some common document events include:

  • load: Triggered when the document is finished loading
  • unload: Triggered when the document is unloaded
  • resize: Triggered when the window is resized
  • scroll: Triggered when the document is scrolled

Miscellaneous events: There are many other types of events that can be handled in JavaScript, including events related to audio and video playback, touch events on mobile devices, and custom events that you can create and dispatch yourself.

In JavaScript, the event object is a special object that is passed as an argument to event handler functions and contains information about the event that occurred. The event object is automatically created by the browser and contains properties and methods that provide information about the event, such as the type of event, the element that triggered the event, and the coordinates of the mouse cursor.

Here’s an example of how you might use the event object in an event handler function:

document.querySelector("button").addEventListener("click", (event) => {
console.log(event.type); // "click"
console.log(event.target); // The <button> element that was clicked
console.log(event.clientX, event.clientY); // The coordinates of the mouse cursor
});

In this example, the event object is passed as an argument to the event handler function and is used to access the type of event that occurred, the element that triggered the event, and the coordinates of the mouse cursor at the time the event was triggered.

The event object also provides several methods that you can use to stop the default behavior of an event, such as preventing a form from being submitted or preventing a link from being followed. For example:

document.querySelector("form").addEventListener("submit", (event) => {
event.preventDefault(); // Prevent the form from being submitted
});

document.querySelector("a").addEventListener("click", (event) => {
event.preventDefault(); // Prevent the link from being followed
});

Contact me here: LinkedIn | Codepen | Email

--

--

Jason Wandrag

With a passion for front end development, I am aiming to make the web a beautiful place by innovating how we consume information and interact with the web.