JavaScript Events Guide

1. Click Events

Definition

Click events occur when a user clicks an element using a pointing device (e.g., mouse or touch).

Important Points

Types

Syntax


element.addEventListener('click', (event) => {
    // Handle click event
});
element.addEventListener('dblclick', (event) => {
    // Handle double click event
});
    

Example


const button = document.querySelector('button');
button.addEventListener('click', (e) => {
    console.log('Button clicked!');
    console.log('Target:', e.target);
});
button.addEventListener('dblclick', (e) => {
    console.log('Button double-clicked!');
});
    

2. Input Events

Definition

Input events are triggered when the value of an input, select, or textarea element changes.

Important Points

Types

Syntax


element.addEventListener('input', (event) => {
    // Handle input change
});
element.addEventListener('change', (event) => {
    // Handle value commitment
});
    

Example


const input = document.querySelector('#username');
const output = document.querySelector('p');
input.addEventListener('input', (e) => {
    output.textContent = e.target.value;
});
input.addEventListener('change', (e) => {
    console.log('Value committed:', e.target.value);
});
    

Difference: input vs change

3. Focus Events

Definition

Focus events occur when an element gains or loses focus.

Important Points

Types

Syntax


element.addEventListener('focus', (event) => {
    // Handle focus
});
element.addEventListener('blur', (event) => {
    // Handle blur
});
    

Example


const input = document.querySelector('#username');
input.addEventListener('focus', (e) => {
    console.log('Input focused');
    e.target.style.backgroundColor = 'lightyellow';
});
input.addEventListener('blur', (e) => {
    console.log('Input blurred');
    e.target.style.backgroundColor = '';
});
    

Difference: focus vs blur

4. Form Events

Definition

Form events are triggered by actions within a form, such as submission.

Important Points

Types

Syntax


form.addEventListener('submit', (event) => {
    event.preventDefault();
    // Handle form submission
});
    

Example


const form = document.querySelector('form');
form.addEventListener('submit', (e) => {
    e.preventDefault();
    const formData = new FormData(form);
    for (const [key, value] of formData.entries()) {
        console.log(`${key}: ${value}`);
    }
});
    

5. Keyboard Events

Definition

Keyboard events occur when a user interacts with the keyboard.

Important Points

Types

Syntax


window.addEventListener('keydown', (event) => {
    // Handle key press
});
window.addEventListener('keyup', (event) => {
    // Handle key release
});
    

Example


window.addEventListener('keydown', (e) => {
    console.log('Key:', e.key, 'Code:', e.code);
    if (e.key === 'Enter') {
        console.log('Enter key pressed!');
    }
});
window.addEventListener('keyup', (e) => {
    console.log('Key released:', e.key);
});
    

Difference: keydown vs keyup vs keypress