Episode 1 — Fundamentals / 1.15 — Event Handling in JavaScript
1.15.c — Forms, Inputs & preventDefault
In one sentence: Forms gather user data — you read values from controls, validate before sending, and use
preventDefault()on thesubmitevent when you want to handle submission in JavaScript instead of a full page reload.
Navigation: ← 1.15.b — Mouse, Keyboard, Scroll · 1.15.d — classList & Events →
1. Accessing form data
Named elements on the form
<form id="signup">
<input name="email" type="email" />
<button type="submit">Join</button>
</form>
const form = document.querySelector("#signup");
const email = form.elements.email.value; // or form.elements["email"]
HTMLFormElement.elements is like a collection of named controls — fast for small forms.
FormData (modern, great for fetch)
form.addEventListener("submit", (e) => {
e.preventDefault();
const data = new FormData(form);
const email = data.get("email");
});
2. Validating forms
| Layer | Role |
|---|---|
| HTML5 | required, type="email", minlength, pattern — browser blocks submit if invalid |
| JS | Custom rules, async checks, show inline errors |
form.addEventListener("submit", (e) => {
e.preventDefault(); // SPA / fetch path — you own submission
if (!form.checkValidity()) {
form.reportValidity(); // show built-in validation UI
return;
}
// …valid: send with fetch(), etc.
});
checkValidity() / reportValidity() integrate with built-in constraint validation.
3. submit event and preventDefault()
form.addEventListener("submit", (event) => {
event.preventDefault(); // stop full page navigation / reload
// send with fetch(), show SPA route, etc.
});
Without preventDefault, the browser performs the default submit (GET/POST navigation or download depending on action / method).
4. change vs input
| Event | When |
|---|---|
change | Control loses focus after value changed (<select>, text after blur), or committed choice |
input | Every keystroke in <input>, <textarea>, contenteditable — live search, character counters |
emailInput.addEventListener("input", () => {
console.log(emailInput.value);
});
5. onsubmit, onchange (handler attributes / properties)
HTML:
<form onsubmit="return false;"> <!-- legacy pattern — avoid -->
JS property:
form.onsubmit = (e) => { e.preventDefault(); };
| Approach | Pros / cons |
|---|---|
addEventListener | Multiple listeners, easier testing, separation of concerns — preferred |
onsubmit / onchange attributes | Quick demos; mixes behavior in HTML; CSP / security concerns with inline strings — avoid in production |
.onsubmit = fn | Only one handler at a time (overwrites) |
Curriculum note: know onsubmit / onchange for reading legacy code and exams; write new code with addEventListener.
6. Key takeaways
FormData+fetchis the modern data path;elementsis fine for small forms.submit+preventDefault= SPA / AJAX submission pattern.inputfor live feedback;changefor committed values.
Explain-It Challenge
Explain without notes:
- Why call
preventDefault()onsubmitwhen posting withfetch? - When would you listen to
inputinstead ofchangeon a text field? - Why is
addEventListenerusually better thanform.onsubmit = …?
Navigation: ← 1.15.b — Mouse, Keyboard, Scroll · 1.15.d — classList & Events →