Animations

API

Arrays

Async

Basics

Challenges

Classes

Console

Dates

Debugging

DOM Elements

DOM Methods

DOM Navigation

DOM Properties

Event Listeners

Flow Control

Forms

Functions

Global Functions

JSON

Keywords

Libraries (3rd party)

Math

Modules

Objects

Snippets

String

Types

Widgets

Window Object

JavaScript - Prevent Default

The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur. Not all events are cancelable. Use the cancelable property to find out if an event is cancelable.

https://www.w3schools.com/jsref/event_preventdefault.asp
https://developer.mozilla.org/en-US/docs/Web/API/Event/preventDefault


Please click on the checkbox control.



document.querySelector("#id-checkbox").addEventListener("click", function (event)
{
    document.getElementById("output-box").innerHTML += "Sorry! <code>preventDefault()</code> won't let you check this!<br />";
    event.preventDefault();
}, false);