The preventDefault()
method will prevent the link above from following the URL.
Click the link and check if the default action is prevented.
<!DOCTYPE html> <html> <body> <p id="demo"></p> <a id="myAnchor" href="https://java2s.com/">Go to java2s.com</a> <script> document.getElementById("myAnchor").addEventListener("click", function(event){ event.preventDefault()/*from w w w . j a v a 2 s . co m*/ document.getElementById("demo").innerHTML = "Was preventDefault() called: " + event.defaultPrevented; }); </script> </body> </html>
The defaultPrevented event property checks whether the preventDefault()
method was called for the event.
The defaultPrevented event property return a Boolean indicating whether the preventDefault()
method was called for the event.
Possible values:
preventDefault()
method was called for the event preventDefault()
method was not called for the event