In JavaScript, the browser and the DOM provide a large number of available events.
For example, this allows us to react to occurrences such as the completion of a document load, or a user’s action with the mouse or keyboard.
Let’s look at some of the categories of events, along with their most common events 👇.
Window Events
These events are related to the window object and its state:
| Event | Triggered When |
|---|---|
| load | The window’s content and all its resources have finished loading. |
| resize | The window size changes. |
| scroll | The window’s content is scrolled. |
| unload | The window is closed or navigated to a new page. |
window.addEventListener('resize', function(event) {
console.log('Window size changed');
});
Load and Unload Events
These events are related to the loading and unloading of resources:
| Event | Triggered When |
|---|---|
| DOMContentLoaded | The DOM content has been loaded and parsed, but before stylesheets, images, and subframes are loaded. |
| beforeunload | Before the page is unloaded, allowing a confirmation message to be shown. |
Example:
document.addEventListener('DOMContentLoaded', function(event) {
console.log('DOM fully loaded and parsed');
});
Mouse Events
These events are related to actions performed with the mouse. Some of the most common mouse events are:
| Event | Triggered When |
|---|---|
| click | The user clicks on an element. |
| dblclick | The user double-clicks on an element. |
| mousedown | The user presses a mouse button over an element. |
| mouseup | The user releases a mouse button over an element. |
| mousemove | The user moves the mouse over an element. |
| mouseenter | The mouse enters the area of an element. |
| mouseleave | The mouse leaves the area of an element. |
document.getElementById('myButton').addEventListener('click', function(event) {
alert('Button clicked');
});
Drag and Drop Events
These events are related to dragging and dropping elements on a web page.
They are commonly used to implement interactive interfaces that allow moving elements from one place to another.
| Event | Triggered When |
|---|---|
| dragstart | The user begins to drag an element. |
| drag | The user drags an element (continuously triggered while the element is being dragged). |
| dragend | The user releases the element they were dragging. |
| dragenter | A dragged element enters the area of a valid drop target. |
| dragover | A dragged element is moved within the area of a valid target. |
| dragleave | A dragged element leaves the area of a valid drop target. |
| drop | A dragged element is dropped on a valid drop target. |
const draggableElement = document.getElementById('draggable');
const container = document.getElementById('container');
// Set up the draggable element
draggableElement.addEventListener('dragstart', function(event) {
event.dataTransfer.setData('text', event.target.id);
console.log('Drag started');
});
// Set up the container as a valid drop target
container.addEventListener('dragover', function(event) {
event.preventDefault(); // Necessary to allow the drop
console.log('Dragged element over the container');
});
container.addEventListener('drop', function(event) {
event.preventDefault();
const id = event.dataTransfer.getData('text');
const element = document.getElementById(id);
container.appendChild(element);
console.log('Element dropped in the container');
});
Keyboard Events
These events are related to actions performed with the keyboard:
| Event | Triggered When |
|---|---|
| keydown | The user presses a key. |
| keyup | The user releases a key. |
| keypress | The user presses a key that produces a character. |
document.addEventListener('keydown', function(event) {
console.log(`Key pressed: ${event.key}`);
});
Form Events
These events are related to interaction with HTML forms:
| Event | Triggered When |
|---|---|
| submit | A form is submitted. |
| change | The value of a form field changes. |
| input | The user enters or modifies the value of a field. |
| focus | A form field receives focus. |
| blur | A form field loses focus. |
document.getElementById('myForm').addEventListener('submit', function(event) {
event.preventDefault(); // Prevents the form from being submitted
console.log('Form submitted');
});
Media Events
These events are related to media playback (audio and video):
| Event | Triggered When |
|---|---|
| play | Media playback starts. |
| pause | Media playback is paused. |
| ended | Media playback ends. |
| volumechange | The volume of the media changes. |
const videoElement = document.getElementById('myVideo');
videoElement.addEventListener('play', function(event) {
console.log('The video is playing');
});