HTML defaultPrevented Event Property



The defaultPrevented event property in HTML is used to check whether the preventDefault() method was called or not. It returns a boolean value i.e. true if the preventDefault() method was called, else false.

Following is the syntax −

event.defaultPrevented

Let us now see an example to implement the defaultPrevented event property in HTML −

Example

 Live Demo

<!DOCTYPE html> <html> <body> <h2>Demo Heading</h2> <a id="myid" href="https://example.com/">Demo (click me)</a> <script>    document.getElementById("myid").addEventListener("click", function(event){       event.preventDefault()       alert("Was preventDefault() called: " + event.defaultPrevented);    }); </script> </body> </html>

Output

Click on the link and the following would get displayed in the alert box −

Updated on: 2019-07-30T22:30:26+05:30

176 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements