To prevent the default action of an event, you can call the Event.preventDefault() method. This method cancels the event if it is cancelable:


Note that the preventDefault() method does not prevent further propagation of an event through the DOM. To explicitly stop the event propagation, use the stopPropagation() method in the event handler.

Let us say you have got the following HTML code snippet:

<form action="/signup" method="GET" id="forms">
    <button id="signup" type="submit">Sign Up</button>

When you click on the button, the HTML <form> is submitted.

To prevent the button from submitting the form, just call the preventDefault() method in the button's event handler:

const btn = document.querySelector('#signup');

btn.addEventListener('click', (e) => {

    alert('Unable to submit the form.');

The Event.preventDefault() method works in all modern browsers, and IE9 and above.

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.