jQuery | event.preventDefault() Method

The preventDefault() Method in jQuery is used to stop the default action of selected element to occur. It is also used to check whether preventDefault() method is called for the selected element or not.

Syntax:

event.preventDefault()

Parameter: It does not accept any parameter.

Return Value: It returns the selected element with the applied change.

Example 1: This example uses preventDefault() method to stop to open the new link.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   
<head>
    <title>
        jQuery event.preventDefault() Method
    </title>
      
    <script src=
    </script>
      
    <style>
        body {
            width: 50%;
            height: 40%;
            padding: 20px;
            border: 2px solid green;
            font-size: 20px;
        }
    </style>
      
    <!-- Script to use event.preventDefault() method -->
    <script>
        $(document).ready(function() {
            $("a").click(function(event) {
                event.preventDefault();
                alert("The required page will not be open");
            });
        });
    </script>
</head>
   
<body>
    <a href="https://www.geeksforgeeks.org">
        Welcome to GeeksforGeeks!
    </a>
</body>
   
</html>

chevron_right


Output:
Before Click on the link:

After click on the link:

Example 2: This example uses event.preventDefault() method to stop submitting the form.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
   
<head>
      
    <script src=
    </script>
      
    <style>
        body {
            width: 50%;
            height: 40%;
            padding: 20px;
            border: 2px solid green;
            font-size: 20px;
        }
        input {
            width: 220px;
            height: 30px;
            border-radius: 10px;
        }
    </style>
      
    <!-- Script to use event.preventDefault() method -->
    <script>
        $(document).ready(function() {
            $("button").click(function(event) {
                event.preventDefault();
                alert("This form will not submit");
            });
        });
    </script>
</head>
   
<body>
   
    <input type="text" placeholder="enter name" />
      
    <br><br>
      
    <button>Submit </button>
</body>
   
</html>

chevron_right


Output:
Before Click on the button:

After Click on the button:



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.