Skills practised: Forms, Form events and submission.
04Explanation
The input event fires on every change to a field's value: each key typed, each paste. Use it for live feedback such as character counts or instant search.
The change event fires when the person commits a change: leaving a text box, picking a select option, ticking a checkbox.
The submit event fires on the <form> when the person presses Enter in a field or clicks the submit button. Listen on the form so both ways are handled.
Without event.preventDefault(), submitting a form sends it and loads a new page. In JavaScript apps you cancel that and handle the data yourself.
form.requestSubmit() behaves like a real submission: it runs validation and fires submit. form.submit() skips both, so listeners never run.
A <button> inside a form defaults to type="submit". Give non-submit buttons type="button" or they will submit the form.
event.submitter tells you which button submitted the form, useful for Save versus Save as draft.
For expensive work on input (network search), debounce: wait until typing pauses (Lesson 77 covers timers) instead of running on every key.
05Definitions
input event
Fires every time a field's value changes.
change event
Fires when a change is committed, such as leaving a field or choosing an option.
submit event
Fires on a form when it is submitted with Enter or a submit button.
requestSubmit
A form method that submits the form the way a user would, including validation and the submit event.
form.reset
A method that puts every control back to its initial value.
Three exercises: explore and modify, complete the implementation, then build independently. Tests check behaviour, so any correct solution passes.
The form reloads the page
Cancel the default submission.
let signedIn = null;
const form = document.querySelector("#login");
form.addEventListener("submit", () => {
signedIn = form.elements.user.value;
});
form.requestSubmit();
console.log("Signed in as", signedIn);
Live filter
React to every keystroke with input.
const filter = document.querySelector("#filter");
const items = [...document.querySelectorAll("#fruits li")];
// TODO: on input, hide every li whose text does not include the filter
// text (ignore case)
filter.value = "ap";
filter.dispatchEvent(new Event("input"));
console.log(items.filter((li) => !li.hidden).map((li) => li.textContent).join(", "));
A comment form
Handle submit, validate emptiness and reset.
// On submit: prevent the default, ignore text that is empty after trim,
// otherwise add an li with the trimmed text to #comments and reset the form.
// Then submit " Nice post! " and an empty comment, and log the count.
09Prediction
Commit to an answer first, then run the real code to compare.
Product search shows suggestions while you type (input, debounced), saves filters when a drop-down changes (change), and runs the full search when you press Enter (submit with preventDefault). Each event matches a different moment in the interaction.
4. What does form.submit() skip that form.requestSubmit() does not?
Nothing
Validation and the submit event
Sending data
Resetting
5. A field has input and change listeners that log their names. The code sets value and dispatches input twice, then change once. What is logged?
input input change
change
input change
change input input
6. A search box calls a slow server. Which is better?
Call the server on every input event
Debounce input events, and search immediately on submit
Only search on page load
Poll the field every 10 ms
13Solutions and tests
Each exercise's solution appears in the exercise once you have run its tests. Solutions are one possible answer: the tests accept any implementation with the same behaviour.
Automated acceptance tests for this lesson
These are the same checks the exercises run. They are visible on purpose: client-side tests help you learn and are not secret.
The form reloads the page: Prints Signed in as ada; signedIn is set; The default submission is cancelled
Live filter: Shows matching fruit; Empty filter shows all
A comment form: Prints 1; Text is trimmed; The form was reset
The lesson lives in the companion repository at curriculum/level-03-interactive-web-applications/module-13-forms-and-validation/lesson-062-input-events-and-form-submission/. Every example on this page is generated from those files.
These links open the files in the GitHub repository (https://github.com/lsvekis/JavaScript-Professional-Engineering-Academy). You can also use the ZIP download above.
15Summary and next steps
input fires on every edit; change fires when the edit is committed.
Listen for submit on the form and call preventDefault.
requestSubmit simulates a real submission; buttons default to submit.
Copy a prompt into the AI assistant of your choice. Then verify what it says by running code here. No AI service is connected to this page.
Concept explanationform events
Context: I am learning form events in JavaScript.
Task: Explain input, change and submit events and preventDefault with a small form.
Constraints: Beginner-friendly language, short runnable examples, no libraries.
Expected behaviour: I can explain the idea in my own words and predict small programs.
Testing: Finish with three quick questions for me to answer.
Code predictionform events drill
Context: I have practised form events.
Task: Give me six form-event programs and ask what each logs and in which order.
Constraints: Wait for my answer before revealing each result.
Expected behaviour: Each question has exactly one correct output.
Testing: Explain any I miss with a step-by-step trace.
Debuggingform events bug
Context: My form reloads the page whenever I press Enter.
Task: Help me find the cause with questions instead of giving me the fix.
Constraints: Do not rewrite my program.
Expected behaviour: I locate and fix the bug myself.
Testing: Suggest the console.log lines that would reveal the problem.
Exercise generationform events practice
Context: I finished the form events exercises.
Task: Create three exercises that use input, change and submit with exact expected output.
Constraints: Fictional data and exact expected output.
Expected behaviour: Each exercise has tests I can run.
Testing: Put the solutions in a separate hidden section.
Code reviewform events review
Context: Here is my code that uses form events.
Task: Review my form handling code for click-instead-of-submit and missing preventDefault.
Constraints: Explain each suggestion briefly and keep behaviour identical.
Expected behaviour: The code is clearer and has no hidden mutation or edge-case bug.
Testing: List the changes in order of importance.