JavaScriptEasy
How do you prevent the default behavior of an event?
By FrontendPro Editorial Team Updated 8/8/2026
#JavaScript
Answer
To prevent the default behavior of an event in JavaScript, you can use the preventDefault method on the event object. For example, if you want to prevent a form from submitting, you can do the following:
js
document.querySelector("form").addEventListener("submit", function (event) {
event.preventDefault();
});
This method stops the default action associated with the event from occurring.
