Forms are everywhere
Every web app has forms — login, signup, search, settings, creating content. In vanilla JavaScript, you handle forms by listening for thesubmit event, reading input values, and sending data to your backend.
Getting form values
Individual input values
Different input types
Input values are always strings, even from
<input type="number">. Convert them with Number() when you need a number. The checked property on checkboxes is a boolean.FormData — read all values at once
FormData collects all named inputs in a form into a single object:
Handling form submission
The standard pattern: listen forsubmit, prevent the default page reload, read values, and send to your API.
Real-time input handling
The input event — fires on every keystroke
The change event — fires when input loses focus
Basic validation
HTML validation attributes
The browser has built-in validation. Use HTML attributes first:JavaScript validation
For more complex rules, validate in your submit handler:Disabling the submit button
Common mistakes
Forgetting preventDefault on form submit
Forgetting preventDefault on form submit
Reading values at load time instead of submit time
Reading values at load time instead of submit time
Missing name attributes on inputs
Missing name attributes on inputs
What’s next?
You can handle forms and user input. Let’s wrap up the DOM & Browser section with localStorage — persisting data between page loads.Local storage
Persist data in the browser between page loads