Ask any question about JavaScript here... and get an instant response.
Post this Question & Answer:
How can I prevent default form submission in JavaScript?
Asked on Apr 02, 2026
Answer
To prevent the default form submission in JavaScript, you can use the "preventDefault" method within an event listener for the form's "submit" event.
<!-- BEGIN COPY / PASTE -->
document.querySelector("form").addEventListener("submit", function(event) {
event.preventDefault();
// Your custom logic here
console.log("Form submission prevented.");
});
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- Add an event listener to the form using "addEventListener" with the "submit" event.
- Call "event.preventDefault()" inside the event listener function to stop the form from submitting.
- You can add any custom logic you need within the event listener function.
- This approach ensures that the form's default action is not executed, allowing you to handle the data as needed.
Recommended Links:
