jQuery | event.isDefaultPrevented() Method

The isDefaultPrevented() Method is an inbuilt method in jQuery which checks whether the preventDefault() method was called for the event. This method returns a boolean value. It returns True if preventDefault() is called on the event, False otherwise.

Syntax:

event.isDefaultPrevented()

Parameters: It accepts a single parameter event which comes from event binding function.

Return Value: It returns True if preventDefault() function called on the event, otherwise it returns false.

Example-1: This example check isPreventDefault() Method called on the event or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!doctype html>
<html>
      
<head>
    <title>
        isPreventDefault() Method
    </title>
      
    <script src=
    </script>
</head>
  
<body>
    <a href="https://www.geeksforgeeks.org">
        Go to Homepage
    </a>
      
    <div id="initial"></div>
    <div id="prevented"></div>
    <div id="response"></div>
      
    <!-- Script to check preventDefault() Method called or not -->
    <script>
        $( "a" ).click(function( event ) {
              
            $( "#initial" ).html( "Before: isDefaultPrevented? <strong>"
                    +event.isDefaultPrevented()+"</strong>");
                      
            event.preventDefault();
              
            $( "#prevented" ).html( "preventDefault() is called now.");
              
            $( "#response" ).html( "So, you are not going anywhere."
                    + " isDefaultPrevented? <strong>"
                    + event.isDefaultPrevented() + "</strong>");
        });
    </script>
</body>
  
</html>                    

chevron_right


Output:
Before Click on the link:
Initial stage of page
After click on the link:
After clicking on the link

Note: The bold text (true/false) is the value of isDefaultPrevented() method.

Example 2: This example check isDefaultPrevented() method prevent default action or not.

filter_none

edit
close

play_arrow

link
brightness_4
code

<!doctype html>
<html>
      
<head>
    <title>
        isPreventDefault() Method
    </title>
    <script src=
    </script>
</head>
  
<body>
  
    <form action = "action.php">
        Input:<br>
        <input type="text" name="input_1">
          
        <button type="submit">Submit</button>
          
    </form
      
    <!-- Script to describe isDefaultPrevented() Method -->
    <script>
        $( "button" ).click(function( event ) {
            if(event.isDefaultPrevented())
                alert('Default action was prevented');
            else
                alert('Click Ok');
          
            event.preventDefault();
          
            if(event.isDefaultPrevented())
                alert('Default action was prevented');
            else
                alert('Click Ok');
        });
    </script>
</body>
  
</html>                    

chevron_right


Output:
Before Click on the submit button:

After Click on the submit button:

Default event prevented after the above step:



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.