Language: EN

javascript-eventos-dom-mas-usados

Most Used DOM Events in JavaScript

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:

EventTriggered When
loadThe window’s content and all its resources have finished loading.
resizeThe window size changes.
scrollThe window’s content is scrolled.
unloadThe 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:

EventTriggered 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 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:

EventTriggered 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');
});

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.

EventTriggered When
dragstartThe user begins to drag an element.
dragThe user drags an element (continuously triggered while the element is being dragged).
dragendThe user releases the element they were dragging.
dragenterA dragged element enters the area of a valid drop target.
dragoverA dragged element is moved within the area of a valid target.
dragleaveA dragged element leaves the area of a valid drop target.
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');
});

Keyboard Events

These events are related to actions performed with the keyboard:

EventTriggered 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}`);
});

Form Events

These events are related to interaction with HTML forms:

EventTriggered 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');
});

Media Events

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

EventTriggered When
playMedia playback starts.
pauseMedia playback is paused.
endedMedia playback ends.
volumechangeThe volume of the media changes.
const videoElement = document.getElementById('myVideo');

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