Mastering DOM Events in JavaScript: A Comprehensive Guide — Part 2

Ayushmaan Srivastav
2 min readFeb 23, 2024

--

Welcome to our in-depth exploration of DOM events in JavaScript. Understanding how to handle events is crucial for building interactive and dynamic web applications. In this guide, we’ll cover various aspects of DOM events, including mouse/pointer events, keyboard events, and form events. We’ll also delve into the nuances of event listeners, form data extraction, and the change and input events.

Main Mouse Events

click Event: The click event occurs when a user clicks on an element.

element.addEventListener(‘click’, function(event) {
console.log(‘Element clicked!’);
});

mousedown Event: mousedown is triggered when the mouse button is pressed down over an element.

element.addEventListener(‘mousedown’, function(event) {
console.log(‘Mouse button pressed down!’);
});

mouseup Event: The mouseup event fires when the mouse button is released.

element.addEventListener(‘mouseup’, function(event) {
console.log(‘Mouse button released!’);
});

mousemove Event: mousemove occurs when the mouse pointer is moved over an element.

element.addEventListener(‘mousemove’, function(event) {
console.log(‘Mouse moved!’);
});

Event Listeners

Event listeners are essential for handling various events in a structured way.

element.addEventListener(‘click’, function(event) {
console.log(‘Event handled!’);
});

Keyboard Events

Overview

Keyboard events enable you to capture user input from the keyboard, offering dynamic possibilities for user interaction.

Main Keyboard Events

keydown Event: Triggered when a key is pressed down.

document.addEventListener(‘keydown’, function(event) {
console.log(‘Key pressed:’, event.key);
});

keyup Event: Fired when a key is released.

document.addEventListener(‘keyup’, function(event) {
console.log(‘Key released:’, event.key);
});

Form Events

Forms play a crucial role in web applications, and handling form events is vital for validation and submission.

Main Form Events

submit Event: Activated when a form is submitted.

form.addEventListener(‘submit’, function(event) {
event.preventDefault();
console.log(‘Form submitted!’);
});

reset Event: Triggered when a form is reset.

form.addEventListener(‘reset’, function(event) {
console.log(‘Form reset!’);
});

Extracting Form Data

Fetching data from forms is a common requirement in web development. The FormData object simplifies this process.

form.addEventListener(‘submit’, function(event) {
event.preventDefault(); // Prevents the default form submission
const formData = new FormData(form);

// Access form data using formData
for (const [key, value] of formData.entries()) {
console.log(`${key}: ${value}`);
}
});

Change Event and Input Event

Change Event

The change event is triggered when the value of an input element changes.

input.addEventListener(‘change’, function(event) {
console.log(‘Value changed:’, event.target.value);
});

Input Event

The input event is fired when the value of an input element is altered, including during typing.

input.addEventListener(‘input’, function(event) {
console.log(‘Input detected:’, event.target.value);
});

By mastering these concepts and events, you’ll be well-equipped to build responsive and user-friendly web applications. Remember to experiment with examples and apply these techniques in your projects for a hands-on learning experience.

--

--

No responses yet