event.preventDefault() Method - Javascript jQuery Method and Property

Javascript examples for jQuery Method and Property:event.preventDefault

Description

The event.preventDefault() method stops the default action from happening.

You can use event.isDefaultPrevented() method to check if the preventDefault() method was called for the event.

For example you can use event.isDefaultPrevented() method to:

  • Prevent a submit button from submitting a form
  • Prevent a link from following the URL

Syntax

Parameter Require Description
event Required. The event parameter comes from the event binding function

The following code shows how to Prevent a link from opening the URL:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    $("a").click(function(event){
        event.preventDefault();/*  w w  w  .  j  a  v  a2 s  .  c o m*/
    });
});
</script>
</head>
<body>

<a href="http://java2s.com/">Go to site</a>

</body>
</html>

Related Tutorials