Course contentsShow
← Web Development
Lesson 733 of 2,98213. DOM EventsFree lesson

Form Events: submit, change, input

Handle form submission and distinguish between input (fires on every change) and change (fires on blur).

Form Events: submit, change, input

You'll learn how to handle form submissions and understand when input versus change events fire.

The Core Idea

Forms generate specific events as users interact with them. Understanding the difference between these events helps you respond at the right moment:

  • submit fires when a form is submitted (button click or Enter key)
  • input fires every single time a field's value changes (every keystroke, paste, etc.)
  • change fires only when a field loses focus and its value has changed

Think of it like texting someone: input is like watching each letter appear as they type in real-time, change is like waiting until they've finished typing and moved on, and submit is when they finally hit Send.

When to Use Each Event

Use submit to validate and process the entire form before sending data. Always call preventDefault() on the event to stop the default page refresh:

const form = document.querySelector('form');
form.addEventListener('submit', (event) => {
  event.preventDefault();
  // Now handle form data
});

Use input for instant feedback like character counters or live search suggestions—it fires with every keystroke.

Use change for dropdowns, checkboxes, or when you only care about the final value after the user finishes editing (like validating an email field after they tab away).

Practical Example

const emailField = document.querySelector('#email');
const messageField = document.querySelector('#message');

// Fires continuously as user types
messageField.addEventListener('input', (event) => {
  console.log('Current length:', event.target.value.length);
});

// Fires only when field loses focus
emailField.addEventListener('change', (event) => {
  console.log('Final email:', event.target.value);
});

Key Takeaway: Use submit for form processing, input for real-time updates, and change for validation after editing is complete.