PreventDefault() Event Method - W3Schools
Có thể bạn quan tâm
Example
Prevent a link from opening the URL:
document.getElementById("myAnchor").addEventListener("click", function(event){ event.preventDefault()}); Try it Yourself »More "Try it Yourself" examples below.
Description
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:
- Clicking on a "Submit" button, prevent it from submitting a form
- Clicking on a link, prevent the link from following the URL
Note: Not all events are 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.
Browser Support
The numbers in the table specify the first browser version that fully supports the method.
| Yes | 9.0 | Yes | Yes | Yes |
Syntax
event.preventDefault()Parameters
| None |
Technical Details
| Return Value: | No return value |
|---|---|
| DOM Version: | DOM Level 2 Events |
More Examples
Example
Prevent the default action of a checkbox:
document.getElementById("myCheckbox").addEventListener("click", function(event){ event.preventDefault()}); Try it Yourself » ❮ Previous Event Methods Next ❯ ★ +1 Sign in to track progressTừ khóa » Html Form Block Submit
-
How To Prevent Form From Being Submitted? - Stack Overflow
-
Prevent HTML
-
Prevent Form Submission JavaScript | Delft Stack
-
Html Form Prevent Submit Code Example - Code Grepper
-
Form Onsubmit Prevent Default Code Example - Code Grepper
-
2 Methods To Prevent Form From Submission - HTML & Javascript
-
How To Prevent Buttons From Causing A Form To Submit With HTML
-
HTMLFormElement: Submit Event - Web APIs | MDN
-
How To Disable The Submit Button While A Form Is Submitting In Webflow
-
HTML DOM Input Submit Disabled Property - W3Schools
-
How To Stop A Form Submit Action Using JQuery ? - GeeksforGeeks
-
JavaScript Stop Form Submit - Daily Dev Tips
-
Javascript: Prevent Double Form Submission - The Art Of Web
-
Html Form Prevent Submit When Hit Enter On Specific Input