LESSON 28
Forms with JavaScript
Learning objective: Read input values and validate forms with JavaScript.
Understand
JavaScript can read and check form input.
Listen for the form's submit event, call event.preventDefault() to stop a page reload, then read input values with .value. You can validate the data and show messages — all on the front end.
Analogy: This is like a receptionist checking a form is complete before accepting it.
See It in Action
Reading a value on submit:
form.addEventListener("submit", (e) => {
e.preventDefault();
const name = input.value;
console.log(name);
});How it works:
preventDefault stops the default reload, then input.value reads what the user typed for validation.Try It Yourself
- Listen for a form's submit event.
- Call
preventDefault(). - Read and log an input's value.
Quick Quiz
Why call event.preventDefault() on submit?
Challenge
Read a name from a form and greet the user without reloading.
Success condition: Submitting the form shows a greeting and does not reload the page.
+10 XP
Lesson Complete
You answered correctly and completed the required practice. Your progress has been saved on this device.
Continue to next lesson →