Predict Event Prevention
Predict Output Medium
Question
What happens when the form is submitted with this handler?
Code
document.querySelector("form").addEventListener("submit", (e) => {
e.preventDefault();
console.log("Form submission prevented");
});
Hint
preventDefault stops the default browser behavior.
Explanation
e.preventDefault() stops the default action of the event. For a form submit, the default is to reload the page (or navigate to the action URL). By calling preventDefault, the page does not reload, and the console.log runs. This is the standard pattern for handling form submissions with JavaScript (e.g., for AJAX submission).
Related Resources
Related Lessons
Look up unfamiliar terms
A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.
Decode error messages
Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.
Build real projects
Apply what you learned by building guided projects with starter code and solutions.