-
Notifications
You must be signed in to change notification settings - Fork 23.2k
Expand file tree
/
Copy pathindex.md
More file actions
96 lines (64 loc) · 2.72 KB
/
index.md
File metadata and controls
96 lines (64 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---
title: "Event: preventDefault() method"
short-title: preventDefault()
slug: Web/API/Event/preventDefault
page-type: web-api-instance-method
browser-compat: api.Event.preventDefault
---
{{APIRef("DOM")}}{{AvailableInWorkers}}
The **`preventDefault()`** method of the {{domxref("Event")}} interface tells the {{Glossary("user agent")}} that the event is being explicitly handled, so its default action, such as page scrolling, link navigation, or pasting text, should not be taken.
The event continues to propagate as usual,
unless one of its event listeners calls
{{domxref("Event.stopPropagation", "stopPropagation()")}}
or {{domxref("Event.stopImmediatePropagation", "stopImmediatePropagation()")}},
either of which terminates propagation at once.
As noted below, calling **`preventDefault()`** for a
non-cancelable event, such as one dispatched via
{{domxref("EventTarget.dispatchEvent()")}}, without specifying
`cancelable: true` has no effect.
If a passive listener calls `preventDefault()`, nothing will happen and a console warning may be generated.
> [!NOTE]
> Look for better alternatives than using `preventDefault()` to block default actions. For example, you can use the `disabled` or `readonly` attribute on a form control to prevent it from being interacted with, use [HTML constraint validation](/en-US/docs/Web/HTML/Guides/Constraint_validation) to reject invalid input, or use the {{cssxref("overflow")}} property to prevent scrolling.
## Syntax
```js-nolint
preventDefault()
```
### Parameters
None.
### Return value
None ({{jsxref("undefined")}}).
## Examples
### 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:
#### JavaScript
```js
const checkbox = document.querySelector("#id-checkbox");
checkbox.addEventListener("click", checkboxClick);
function checkboxClick(event) {
const warn = "preventDefault() won't let you check this!\n";
document.getElementById("output-box").innerText += warn;
event.preventDefault();
}
```
#### HTML
```html
<p>Please click on the checkbox control.</p>
<form>
<label for="id-checkbox">Checkbox:</label>
<input type="checkbox" id="id-checkbox" />
</form>
<div id="output-box"></div>
```
#### Result
{{EmbedLiveSample("Blocking_default_click_handling")}}
## Notes
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 {{domxref("Event.cancelable")}} to check if the event is cancelable.
Calling `preventDefault()` for a non-cancelable event has no effect.
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}