How to prevent the browser from executing the default action in jQuery?

jQueryWeb DevelopmentFront End Technology

To prevent the browser from executing the default action in  jQuery, use the preventDefault() method. The preventDefault() method  prevents the browser from executing the default action.

Example

You can use the method isDefaultPrevented() to know whether this method was ever called (on that event object).

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>
raja
Published on 19-Dec-2017 19:37:30
Advertisements