javascript-eventos-dom-mas-usados

Most Used DOM Events in JavaScript

  • 4 min

In JavaScript, the browser and the DOM provide a large number of available events.

For example, this allows us to react to events such as the completion of a document’s loading, or a user’s action with the mouse or keyboard.

Let’s look at some of the event categories, with their most common events 👇.

Window Events

These events are related to the window object and its state:

EventTriggers when
loadThe window’s content and all its resources have finished loading.
resizeThe window’s size changes.
scrollThe window’s content is scrolled.
unloadThe window is closed or navigation to a new page occurs.
window.addEventListener('resize', function(event) {
    console.log('Window size changed');
});
Copied!

Load and Unload Events

These events are related to the loading and unloading of resources:

EventTriggers when
DOMContentLoadedThe DOM content has been loaded and parsed, but before stylesheets, images, and subframes are loaded.
beforeunloadBefore the page is unloaded, allowing a confirmation message to be displayed.

Example:

document.addEventListener('DOMContentLoaded', function(event) {
    console.log('DOM fully loaded and parsed');
});
Copied!

Mouse Events

These events are related to actions performed with the mouse. Some of the most common mouse events are:

EventTriggers when
clickThe user clicks on an element.
dblclickThe user double-clicks on an element.
mousedownThe user presses a mouse button over an element.
mouseupThe user releases a mouse button over an element.
mousemoveThe user moves the mouse over an element.
mouseenterThe mouse enters the area of an element.
mouseleaveThe mouse leaves the area of an element.
document.getElementById('myButton').addEventListener('click', function(event) {
    alert('Button clicked');
});
Copied!

Drag and Drop Events

These events are related to drag and drop actions on a web page.

They are commonly used to implement interactive interfaces that allow moving elements from one place to another.

EventTriggers when
dragstartThe user starts dragging an element.
dragThe user drags an element (fires continuously while the element is being dragged).
dragendThe user releases the element being dragged.
dragenterA dragged element enters a valid drop target area.
dragoverA dragged element moves within a valid target area.
dragleaveA dragged element leaves a valid drop target area.
dropA 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');
});
Copied!

Keyboard Events

These events are related to actions performed with the keyboard:

EventTriggers when
keydownThe user presses a key.
keyupThe user releases a key.
keypressThe user presses a key that produces a character.
document.addEventListener('keydown', function(event) {
    console.log(`Key pressed: ${event.key}`);
});
Copied!

Form Events

These events are related to interaction with HTML forms:

EventTriggers when
submitA form is submitted.
changeThe value of a form field changes.
inputThe user enters or modifies the value of a field.
focusA form field receives focus.
blurA form field loses focus.
document.getElementById('myForm').addEventListener('submit', function(event) {
    event.preventDefault(); // Prevents the form from being submitted
    console.log('Form submitted');
});
Copied!

Media Events

These events are related to media playback (audio and video):

EventTriggers when
playMedia playback starts.
pauseMedia playback is paused.
endedMedia playback ends.
volumechangeThe media volume changes.
const videoElement = document.getElementById('myVideo');

videoElement.addEventListener('play', function(event) {
    console.log('The video is playing');
});
Copied!