Prevent a link from opening the URL:
More "Try it Yourself" examples below.
The preventDefault() method cancels the event if it is cancelable, meaning that the default action that belongs to the event will not occur.
For example, this can be useful when:
Note: All events are not cancelable. Use the cancelable property to find out if an event is cancelable.
Note: The preventDefault() method does not prevent further propagation of an event through the DOM. Use the stopPropagation() method to handle this.
The numbers in the table specify the first browser version that fully supports the method.
Method | |||||
---|---|---|---|---|---|
preventDefault() | Yes | 9.0 | Yes | Yes | Yes |
None |
Return Value: | No return value |
---|---|
DOM Version: | DOM Level 2 Events |
Prevent the default action of a checkbox: