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:
submitfires when a form is submitted (button click or Enter key)inputfires every single time a field's value changes (every keystroke, paste, etc.)changefires 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.