What is the difference between event.preventDefault() and return false in jQuery?


Returning false from jQuery event handlers is like calling both preventDefault() and stopPropagation(). The preventDefault() method prevents the browser from executing the default action.

Example

You can try to run the following code to run event.preventDefault() method in jQuery −

Live Demo

<html>

   <head>
      <title>jQuery preventDefault() method</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("a").click(function(event){
               event.preventDefault();
               alert( "Default behavior is disabled!" );
            });
         });
      </script>
   </head>
   
   <body>
      <span>Click the following link and it won't work:</span>
      <a href = "https://www.google.com">GOOGLE Inc.</a>
   </body>
   
</html>

Example

You can try to run the following code to return false from jQuery event handlers −

Live Demo

<html>

   <head>
      <title>jQuery return false</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
       
      <script>
         $(document).ready(function() {
            $("a").click(function(event){
               alert( "Default behavior is disabled!" );
               return false;
            });
         });
      </script>
   </head>
   
   <body>
      <span>Click the following link and it won't work:</span>
      <a href = "https://www.google.com">GOOGLE Inc.</a>
   </body>
   
</html>

Updated on: 14-Feb-2020

489 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements