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 Jan 29, 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();
console.log('Form submission prevented.');
});
<!-- END COPY / PASTE -->Additional Comment:
✅ Answered with JavaScript best practices.- The code selects the first form on the page using "document.querySelector('form')".
- An event listener is added to the form for the "submit" event.
- "event.preventDefault()" is called to stop the form from submitting.
- "console.log" is used to confirm that the form submission was prevented.
Recommended Links:
