/Web APIs

Event: preventDefault() method

The preventDefault() method of the Event interface tells the user agent that if the event does not get explicitly handled, its default action should not be taken as it normally would be.

The event continues to propagate as usual, unless one of its event listeners calls stopPropagation() or stopImmediatePropagation(), either of which terminates propagation at once.

As noted below, calling preventDefault() for a non-cancelable event, such as one dispatched via EventTarget.dispatchEvent(), without specifying cancelable: true has no effect.




Blocking default click handling

Toggling a checkbox is the default action of clicking on a checkbox. This example demonstrates how to prevent that from happening:


const checkbox = document.querySelector("#id-checkbox");

checkbox.addEventListener("click", checkboxClick, false);

function checkboxClick(event) {
  let warn = "preventDefault() won't let you check this!<br>";
  document.getElementById("output-box").innerHTML += warn;


<p>Please click on the checkbox control.</p>

  <label for="id-checkbox">Checkbox:</label>
  <input type="checkbox" id="id-checkbox" />

<div id="output-box"></div>


Stopping keystrokes from reaching an edit field

The following example demonstrates how invalid text input can be stopped from reaching the input field with preventDefault(). Nowadays, you should usually use native HTML form validation instead.


The HTML form below captures user input. Since we're only interested in keystrokes, we're disabling autocomplete to prevent the browser from filling in the input field with cached values.

<div class="container">
  <p>Please enter your name using lowercase letters only.</p>

    <input type="text" id="my-textbox" autocomplete="off" />


We use a little bit of CSS for the warning box we'll draw when the user presses an invalid key:

.warning {
  border: 2px solid #f39389;
  border-radius: 2px;
  padding: 10px;
  position: absolute;
  background-color: #fbd8d4;
  color: #3b3c40;


And here's the JavaScript code that does the job. First, listen for keypress events:

const myTextbox = document.getElementById("my-textbox");
myTextbox.addEventListener("keypress", checkName, false);

The checkName() function, which looks at the pressed key and decides whether to allow it:

function checkName(evt) {
  const charCode = evt.charCode;
  if (charCode !== 0) {
    if (charCode < 97 || charCode > 122) {
        "Please use lowercase letters only.\n" + `charCode: ${charCode}\n`

The displayWarning() function presents a notification of a problem. It's not an elegant function but does the job for the purposes of this example:

let warningTimeout;
const warningBox = document.createElement("div");
warningBox.className = "warning";

function displayWarning(msg) {
  warningBox.innerHTML = msg;

  if (document.body.contains(warningBox)) {
  } else {
    // insert warningBox after myTextbox
    myTextbox.parentNode.insertBefore(warningBox, myTextbox.nextSibling);

  warningTimeout = setTimeout(() => {
    warningTimeout = -1;
  }, 2000);



Calling preventDefault() during any stage of event flow cancels the event, meaning that any default action normally taken by the implementation as a result of the event will not occur.

You can use Event.cancelable to check if the event is cancelable. Calling preventDefault() for a non-cancelable event has no effect.


Browser compatibility

Desktop Mobile
Chrome Edge Firefox Internet Explorer Opera Safari WebView Android Chrome Android Firefox for Android Opera Android Safari on IOS Samsung Internet
preventDefault 1 12 1 9 7 1 4.4 18 4 10.1 1 1.0

© 2005–2023 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.