Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Prevent Browser Default Action in jQuery



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>
Updated on: 2020-02-14T10:41:19+05:30

793 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements