The defaultPrevented
read-only property of the Event
interface returns a Boolean
indicating whether or not the call to Event.preventDefault()
canceled the event.
Note: You should use this instead of the non-standard, deprecated getPreventDefault()
method (see bug 691151).
Syntax
var defaultWasPrevented = event.defaultPrevented;
Value
A Boolean
, where true
indicates that the default user agent action was prevented, and false
indicates that it was not.
Example
This example logs attempts to visit links from two <a>
elements. JavaScript is used to prevent the second link from working.
HTML
<p><a id="link1" href="#link1">Visit link 1</a></p>
<p><a id="link2" href="#link2">Try to visit link 2</a> (you can't)</p>
<p id="log"></p>
JavaScript
function stopLink(event) {
event.preventDefault();
}
function logClick(event) {
const log = document.getElementById('log');
if (event.target.tagName === 'A') {
if (event.defaultPrevented) {
log.innerText = 'Sorry, but you cannot visit this link!\n' + log.innerText;
}
else {
log.innerText = 'Visiting link...\n' + log.innerText;
}
}
}
const a = document.getElementById('link2');
a.addEventListener('click', stopLink);
document.addEventListener('click', logClick);
Result
Specifications
Specification | Status | Comment |
---|---|---|
DOMThe definition of 'Event.defaultPrevented()' in that specification. | Living Standard |
Browser compatibility
The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request.
Update compatibility data on GitHub
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
defaultPrevented
|
Chrome
Full support 18 |
Edge
Full support 12 |
Firefox
Full support 6 |
IE
Full support 9 |
Opera
Full support 11 |
Safari
Full support 5 |
WebView Android
Full support Yes |
Chrome Android
Full support 18 |
Firefox Android
Full support 6 |
Opera Android
Full support 11 |
Safari iOS
Full support 5 |
Samsung Internet Android
Full support 1.0 |
Legend
- Full support
- Full support
Event.defaultPrevented by Mozilla Contributors is licensed under CC-BY-SA 2.5.